首页 > 其他 > 详细

leetcode 104.Maximum Depth of Binary Tree

时间:2015-03-02 18:30:20      阅读:225      评论:0      收藏:0      [点我收藏+]

想用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里面自带的函数了,然后就再次简化。

  总结,从零开始,加油!

leetcode 104.Maximum Depth of Binary Tree

原文:http://www.cnblogs.com/Pillar/p/4309004.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!