104. Maximum Depth of Binary Tree

来源:互联网 发布:淘宝的拼团在哪里 编辑:程序博客网 时间:2024/06/15 19:39
问题描述:

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

解题思路:
给定一棵二叉树,求大的深度。顾名思义,dfs递归求,比较左右子树谁最大:l>r?l+1:r+1
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL) return 0;
            int l,r;
                l=maxDepth(root->left);
                r=maxDepth(root->right);
            return l>r?l+1:r+1;
    }
};