leetcode练习题 maximum-depth-of-binary-tree

解题思路

若结点为空,则返回0,否则计算该结点左右子树的最大深度,最后返回左右子树最大深度的较大值 + 1.即为最终结果。

代码

class Solution {
public:
    int maxDepth(TreeNode *root) {
        if(root == NULL)
            return 0;
        int l = maxDepth(root->left);
        int r = maxDepth(root->right);
        return l > r ? (l + 1) : (r + 1);
    }
};

你可能感兴趣的:(leetcode练习,算法,leetcode,二叉树)