LeetCode--104. Maximum Depth of Binary Tree

题目链接:https://leetcode.com/problems/maximum-depth-of-binary-tree/

求树的最大深度,一言以蔽之:子节点树的最大深度加1就是父节点树的最大深度。递归题目想通了就好简单!!!

class Solution {
    public int maxDepth(TreeNode root) {
        int m=0;
        int n=0;
        if(root==null )
            return 0;
        else
        {
            m=maxDepth(root.left)+1;
            n=maxDepth(root.right)+1;
        }
        return m>n?m:n;
    }
}

猜你喜欢

转载自blog.csdn.net/To_be_to_thought/article/details/84555289
今日推荐