zoukankan      html  css  js  c++  java
  • 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里面自带的函数了,然后就再次简化。

      总结,从零开始,加油!

  • 相关阅读:
    索引的设计与使用
    字符集
    选择合适的数据类型
    java 23种设计模式 深入理解(转)
    进程间的通信方式
    SPRING的事务传播
    面向对象的五大基本原则
    J2EE中常用的名词解释
    JVM内存配置参数:
    域对象
  • 原文地址:https://www.cnblogs.com/Pillar/p/4309004.html
Copyright © 2011-2022 走看看