leetcode 104.Maximum Depth of Binary Tree

想用java去做所有leetcode的题目,从简单开始做,一步一步来,慢慢往前走,加油!

题目:

  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.

解决方案:Runtime: 224 ms

public class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
            return 0;
        //int l, r;
        return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
        //return ((l = maxDepth(root.left)) > (r = maxDepth(root.right))?l:r) + 1;
    }
}

  很简单,但是自己做的时候各种忘,首先就是对于l和r的设置,刚开始没这样写l和r,直接写的是max>max?max:max这样,结果显然不对,因为这样就走了两次max里面的东西,明显让结果不对,之后慢慢修改,然后改成注释的样子,在之后,知道用java里面自带的函数了,然后就再次简化。

  总结,从零开始,加油!

原文地址:https://www.cnblogs.com/Pillar/p/4309004.html