题目

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.

题解

递归解法:

     public int maxDepth(TreeNode root) {
         if(root==null)
             return 0;
         int leftmax = maxDepth(root.left);
         int rightmax = maxDepth(root.right);
         return Math.max(leftmax, rightmax)+1;
     }

非递归解法,参考codeganker(http://codeganker.blogspot.com/2014/02/maximum-depth-of-binary-tree-leetcode.html)的:

 1 public int maxDepth(TreeNode root) {
 2     if(root == null)
 3         return 0;
 4     
 5     int depth = 0;
 6     LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
 7     queue.add(root);
 8     int curNum = 1; //num of nodes left in current level
 9     int nextNum = 0; //num of nodes in next level
     while(!queue.isEmpty()){
         TreeNode n = queue.poll();
         curNum--;
         if(n.left!=null){
             queue.add(n.left);
             nextNum++;
         }
         if(n.right!=null){
             queue.add(n.right);
             nextNum++;
         }
         if(curNum == 0){
             curNum = nextNum;
             nextNum = 0;
             depth++;
         }
     }
     return depth;
 }

Maximum Depth of Binary Tree leetcode java的更多相关文章

  1. Maximum Depth of Binary Tree leetcode

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

  2. Minimum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...

  3. [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  4. LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)

    104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...

  5. LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree

    LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...

  6. LeetCode——Maximum Depth of Binary Tree

    LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...

  7. LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]

    Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...

  8. Leetcode | Minimum/Maximum Depth of Binary Tree

    Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...

  9. 【LeetCode练习题】Maximum Depth of Binary Tree

    Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...

随机推荐

  1. 装饰 Markdown

    利用 Font Awesome 提升 Markdown 的表现能力 Font Awesome 是一个字体和图标工具包,包含人物.动物.建筑.商业.品牌等等各种主题丰富的图标符号,可以通过相应的语法添加 ...

  2. 美团外卖Android平台化的复用实践

    美团外卖平台化复用主要是指多端代码复用,正如美团外卖iOS多端复用的推动.支撑与思考文章所述,多端包含有两层意思:其一是相同业务的多入口,指美团外卖业务需要在美团外卖App(下文简称外卖App)和美团 ...

  3. PHP 笔记——自定义函数

    1. 定义函数 function function_name ([$arg_1],[$arg_2], ... [$arg_n]){ fun_body; [return arg_n;] } 在PHP中, ...

  4. 洛谷.4383.[八省联考2018]林克卡特树lct(树形DP 带权二分)

    题目链接 \(Description\) 给定一棵边带权的树.求删掉K条边.再连上K条权为0的边后,新树的最大直径. \(n,K\leq3\times10^5\). \(Solution\) 题目可以 ...

  5. bzoj 3283 扩展BSGS + 快速阶乘

    T2  扩展BSGS T3 快速阶乘 给定整数n,质数p和正整数c,求整数s和b,满足n! / pb = s mod pc 考虑每次取出floor(n/p)个p因子,然后将问题转化为子问题. /*** ...

  6. Codeforces Round #353 (Div. 2) B. Restoring Painting 水题

    B. Restoring Painting 题目连接: http://www.codeforces.com/contest/675/problem/B Description Vasya works ...

  7. Codeforces Beta Round #11 A. Increasing Sequence 贪心

    A. Increasing Sequence 题目连接: http://www.codeforces.com/contest/11/problem/A Description A sequence a ...

  8. 文件上传demo

    前端代码: <form action="upload.php" enctype="multipart/form-data" method="po ...

  9. chrome --headless --disable-gpu --dump-dom http://www.python.org

    Driving Headless Chrome with Python:Python chrome --headless --disable-gpu --dump-dom http://www.pyt ...

  10. SQLite 一款轻型的数据库

    SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经在很多嵌入式产 ...