forked from DmrfCoder/AlgorithmAndDataStructure
-
Notifications
You must be signed in to change notification settings - Fork 1
/
38.py
38 lines (34 loc) · 933 Bytes
/
38.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def TreeDepth(self, pRoot):
# write code here
if pRoot is None:
return 0
return max(1 + self.TreeDepth(pRoot.left), 1 + self.TreeDepth(pRoot.right))
#迭代版本
'''
class Solution {
public:
int TreeDepth(TreeNode* pRoot) {
if (!pRoot) return 0;
queue<TreeNode*> que;
que.push(pRoot);int depth=0;
while (!que.empty()) {
int size=que.size();
depth++;
for (int i=0;i<size;i++) { //一次处理一层的数据
TreeNode *node=que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return depth;
}
};
'''