LeetCode-Maximum Depth of Binary Tree
来源:互联网 发布:js打开无导航栏新窗口 编辑:程序博客网 时间:2024/06/06 01:32
递归方法非常简单 只要判断左右depth 取max+1就行
public class Solution { public int maxDepth(TreeNode root) { if ( root == null ) return 0; int left = maxDepth(root.left); int right = maxDepth(root.right); return Math.max(left,right)+1; }}
iterative:bfs, 和level order traversal一样 注意level最开始值是0,和mindepth最开始值取1不一样。因为min在循环中碰到叶子就停了 没有最后的加一
public class Solution { public int maxDepth(TreeNode root) { if ( root == null ) return 0; int level = 0; Queue <TreeNode> que = new LinkedList<TreeNode>(); que.offer(root); while ( !que.isEmpty()){ int num = que.size(); for ( int i = 0; i < num; i ++ ){ TreeNode cur = que.poll(); if ( cur.left != null ) que.offer(cur.left); if ( cur.right != null ) que.offer(cur.right); } level ++; } return level; }}
0 0
- Leetcode - Tree - Maximum Depth of Binary Tree
- [leetcode][tree] Maximum Depth of Binary Tree
- LeetCode Maximum Depth of Binary Tree
- [Leetcode] Maximum Depth of Binary Tree
- leetcode 24: Maximum Depth of Binary Tree
- [LeetCode] Maximum Depth of Binary Tree
- Leetcode 104 Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- [LeetCode]Maximum Depth of Binary Tree
- [leetcode]Maximum Depth of Binary Tree
- Leetcode: Maximum Depth of Binary Tree
- LeetCode-Maximum Depth of Binary Tree
- [leetcode] Maximum Depth of Binary Tree
- LeetCode - Maximum Depth of Binary Tree
- LeetCode:Maximum Depth of Binary Tree
- LeetCode | Maximum Depth of Binary Tree
- 【leetcode】Maximum Depth of Binary Tree
- Feedforward Neural Network Language Model(NNLM)原理及数学推导
- Path Sum
- 【C#】Binomial model option定价
- Find Median from Data Stream
- Talking Tom Source Code
- LeetCode-Maximum Depth of Binary Tree
- spring MVC环境搭建
- 在cocos2d-js实现自动绑定cocostudioUI控件与事件(三)
- spring mvc上传、下载的实现
- 【Java】利用Java对配置文件操作实现Java程序的国际化
- [Leetcode]Longest Consecutive Sequence
- UVA - 10891(sum游戏,dp)
- 嵌入式linux学习笔记一--打补丁
- *LeetCode-Binary Search Tree Iterator