1. 程式人生 > >LeetCode--104. Maximum Depth of Binary Tree

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;
    }
}