二叉树的深度

Desicription

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
int TreeDepth(TreeNode* pRoot) {
int maxLevel = 0;
queue<pair<TreeNode*, int>> q;
q.push(make_pair(pRoot, 1));
while(!q.empty()) {
auto current = q.front();
q.pop();
if(current.first == nullptr) {
continue;
}
maxLevel = max(maxLevel, current.second);
q.push(make_pair(current.first->left, current.second + 1));
q.push(make_pair(current.first->right, current.second + 1));
}
return maxLevel;
}
};