题目链接
解题思路
该题我们需要从根节点开始,分别求解左右孩子的深度
最终高度为左右孩子的高度中的较大值加上本层的1
解题代码
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL)
return 0;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
return 1+max(leftDepth, rightDepth);
}
};