JZ38 二叉树的深度

本文最后更新于:2022年4月9日 中午

image-20211008111141752

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot) {
if (pRoot == nullptr) return 0;
int leftDepth = TreeDepth(pRoot->left);
int rightDepth = TreeDepth(pRoot->right);
return max(leftDepth, rightDepth) + 1;
}
};