day16
深度和高度的区别:
深度:任意结点到根节点的距离
高度:任意结点到叶子结点的距离
本题用的是后序遍历:
因为要先知道叶子结点有多深,最后再返回给根节点
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == nullptr)
return 0;
int leftHeight = maxDepth(root->left);
int rightHeight = maxDepth(root->right);
int res = 1 + max(leftHeight,rightHeight);
return res;
}
};