Skip to content

Commit

Permalink
added python solution for 002.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 13, 2017
1 parent 79c0d1c commit 1ec1078
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 13 deletions.
31 changes: 19 additions & 12 deletions 002. Maximum Depth of Binary Tree/README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,33 @@
这道题应该算是基本题了。我顺便复习了一下[二叉树的基本概念](http://en.wikipedia.org/wiki/Binary_tree),并回顾了一下**深度优先遍历****广度优先遍历**两个重点知识。
而这道题就是脱胎于前者的思想。
# 思路(C++)

求 root 的最大深度,那么我就需要求出 left 的最大深度,以及 right 的最大深度。把 root 替换成 left 或 right 又会再重复上面的步骤。
这显然是**递归**
这道题应该算是基本题了。我顺便复习了一下[二叉树的基本概念](http://en.wikipedia.org/wiki/Binary_tree),并回顾了一下**深度优先遍历****广度优先遍历**两个重点知识。而这道题就是脱胎于前者的思想。

求 root 的最大深度,那么我就需要求出 left 的最大深度,以及 right 的最大深度。把 root 替换成 left 或 right 又会再重复上面的步骤。这显然是**递归**

于是我们写个框架出来:

```cpp
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? leftDepth : rightDepth;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? leftDepth : rightDepth;
```

基本思路出来了,就需要考虑几个特殊情况:

1. 叶子节点返回什么? 显然应该是1. (叶子节点的深度应该是1)
2. 如何判断是叶子节点? `left == NULL, right == NULL`
1. 如何判断是叶子节点? `left == NULL, right == NULL`

好了,补充上述框架:

```cpp
if (root == NULL) return 0;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? ++leftDepth : ++rightDepth; // 如果是叶子节点,leftDepth和rightDepth都是0,返回1.
if (root == NULL) return 0;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return leftDepth > rightDepth ? ++leftDepth : ++rightDepth; // 如果是叶子节点,leftDepth rightDepth都是0,返回1.
```

这基本就OK了。但看起来不够简洁啊。最后一步不就是求个最大值?用 STL 得了。

```cpp
if (root == NULL) return 0;
return std::max(maxDepth(root->left), maxDepth(root->right))+1;
Expand All @@ -33,3 +36,7 @@ return std::max(maxDepth(root->left), maxDepth(root->right))+1;
这就是我的最后答案。

PS: C++ 11 更推荐使用 `nullptr` 来代替 `NULL` ,但是原数据结构中使用了 `NULL` ,这里只好妥协。

## Python

思路与 C++ 一致,本来想用迭代的方式写个不一样的,但写完觉得实在太啰嗦了,还是这个解法看起来简洁明了。
33 changes: 33 additions & 0 deletions 002. Maximum Depth of Binary Tree/solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!python3

class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None


class Solution:
def maxDepth(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if root is None:
return 0
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1


if __name__ == "__main__":
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.left = TreeNode(6)
root.left.left.left = TreeNode(7)
root.left.left.right = TreeNode(8)
root.left.left.right.left = TreeNode(9)

print(Solution().maxDepth(root))

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ LeetCode solutions in C++ 11. (From Easy to Hard)
|NO.|Title|Solution|Add Date|Difficulty|
|---|-----|--------|--------|----------|
|1|[Single Number][1]|[C++](001.%20Single%20Number/solution.h)|[Python](001.%20Single%20Number/solution.py)|2014/10/15|Medium|
|2|[Maximum Depth of Binary Tree][2]|[C++](002.%20Maximum%20Depth%20of%20Binary%20Tree/solution.h)|2014/10/16|Easy|
|2|[Maximum Depth of Binary Tree][2]|[C++](002.%20Maximum%20Depth%20of%20Binary%20Tree/solution.h)|[Python](002.%20Maximum%20Depth%20of%20Binary%20Tree/solution.py)|2014/10/16|Easy|
|3|[Same Tree][3]|[C++](003.%20Same%20Tree/solution.h)|2014/10/17|Easy|
|4|[Reverse Integer][4]|[C++](004.%20Reverse%20Integer/solution.h)|2014/10/18|Easy|
|5|[Best Time to Buy and Sell Stock II][5]|[C++](005.%20Best%20Time%20to%20Buy%20and%20Sell%20Stock%20II/solution.h)|2014/10/19|Medium|
Expand Down

0 comments on commit 1ec1078

Please sign in to comment.