Skip to content

Commit 1ec1078

Browse files
committed
added python solution for 002.
1 parent 79c0d1c commit 1ec1078

File tree

3 files changed

+53
-13
lines changed

3 files changed

+53
-13
lines changed
Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,33 @@
1-
这道题应该算是基本题了。我顺便复习了一下[二叉树的基本概念](http://en.wikipedia.org/wiki/Binary_tree),并回顾了一下**深度优先遍历****广度优先遍历**两个重点知识。
2-
而这道题就是脱胎于前者的思想。
1+
# 思路(C++)
32

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

77
于是我们写个框架出来:
8+
89
```cpp
9-
int leftDepth = maxDepth(root->left);
10-
int rightDepth = maxDepth(root->right);
11-
return leftDepth > rightDepth ? leftDepth : rightDepth;
10+
int leftDepth = maxDepth(root->left);
11+
int rightDepth = maxDepth(root->right);
12+
return leftDepth > rightDepth ? leftDepth : rightDepth;
1213
```
1314

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

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

1920
好了,补充上述框架:
21+
2022
```cpp
21-
if (root == NULL) return 0;
22-
int leftDepth = maxDepth(root->left);
23-
int rightDepth = maxDepth(root->right);
24-
return leftDepth > rightDepth ? ++leftDepth : ++rightDepth; // 如果是叶子节点,leftDepth和rightDepth都是0,返回1.
23+
if (root == NULL) return 0;
24+
int leftDepth = maxDepth(root->left);
25+
int rightDepth = maxDepth(root->right);
26+
return leftDepth > rightDepth ? ++leftDepth : ++rightDepth; // 如果是叶子节点,leftDepth rightDepth都是0,返回1.
2527
```
2628

2729
这基本就OK了。但看起来不够简洁啊。最后一步不就是求个最大值?用 STL 得了。
30+
2831
```cpp
2932
if (root == NULL) return 0;
3033
return std::max(maxDepth(root->left), maxDepth(root->right))+1;
@@ -33,3 +36,7 @@ return std::max(maxDepth(root->left), maxDepth(root->right))+1;
3336
这就是我的最后答案。
3437

3538
PS: C++ 11 更推荐使用 `nullptr` 来代替 `NULL` ,但是原数据结构中使用了 `NULL` ,这里只好妥协。
39+
40+
## Python
41+
42+
思路与 C++ 一致,本来想用迭代的方式写个不一样的,但写完觉得实在太啰嗦了,还是这个解法看起来简洁明了。
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!python3
2+
3+
class TreeNode:
4+
def __init__(self, x):
5+
self.val = x
6+
self.left = None
7+
self.right = None
8+
9+
10+
class Solution:
11+
def maxDepth(self, root):
12+
"""
13+
:type root: TreeNode
14+
:rtype: int
15+
"""
16+
if root is None:
17+
return 0
18+
return max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
19+
20+
21+
if __name__ == "__main__":
22+
root = TreeNode(1)
23+
root.left = TreeNode(2)
24+
root.right = TreeNode(3)
25+
root.left.left = TreeNode(4)
26+
root.left.right = TreeNode(5)
27+
root.right.left = TreeNode(6)
28+
root.left.left.left = TreeNode(7)
29+
root.left.left.right = TreeNode(8)
30+
root.left.left.right.left = TreeNode(9)
31+
32+
print(Solution().maxDepth(root))
33+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LeetCode solutions in C++ 11. (From Easy to Hard)
66
|NO.|Title|Solution|Add Date|Difficulty|
77
|---|-----|--------|--------|----------|
88
|1|[Single Number][1]|[C++](001.%20Single%20Number/solution.h)|[Python](001.%20Single%20Number/solution.py)|2014/10/15|Medium|
9-
|2|[Maximum Depth of Binary Tree][2]|[C++](002.%20Maximum%20Depth%20of%20Binary%20Tree/solution.h)|2014/10/16|Easy|
9+
|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|
1010
|3|[Same Tree][3]|[C++](003.%20Same%20Tree/solution.h)|2014/10/17|Easy|
1111
|4|[Reverse Integer][4]|[C++](004.%20Reverse%20Integer/solution.h)|2014/10/18|Easy|
1212
|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|

0 commit comments

Comments
 (0)