Maximum Depth of Binary Tree(java version)
来源:互联网 发布:html5小游戏源码下载 编辑:程序博客网 时间:2024/06/10 19:24
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.
题意是:非递归求二叉树最大深度。
ac代码:
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public int maxDepth(TreeNode root) { if(root==null)return 0; Queue<TreeNode> qu=new LinkedList<TreeNode>(); root.val=1; qu.add(root); int depth=1; while(!qu.isEmpty()){ TreeNode tn=qu.peek(); qu.remove(); depth=tn.val; if(tn.left!=null){ TreeNode t; t=tn.left; t.val=tn.val+1; qu.add(t); } if(tn.right!=null){ TreeNode t; t=tn.right; t.val=tn.val+1; qu.add(t); } } return depth; }}
0 0
- Maximum Depth of Binary Tree(java version)
- [Leetcode] Maximum Depth of Binary Tree (Java)
- Maximum Depth of Binary Tree (Java)
- [LeetCode][Java] Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree Java
- LeetCode|Maximum Depth of Binary Tree-java
- (java)leetcode Maximum Depth of Binary Tree
- leetcode:Maximum Depth of Binary Tree 【Java】
- maximum-depth-of-binary-tree Java code
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- android.intent.category.DEFAULT
- android有的手机拍照后上传图片被旋转的问题
- 黑马程序员_静态方法和实例方法的一些区别
- code::blocks基本使用方法
- uva 10795
- Maximum Depth of Binary Tree(java version)
- linux下mysql默认安装目录和常用命令
- 百度云存储教程
- 50个高端大气上档次的管理后台界面模板
- hibernate.exception.SQLGrammarException异常:could not insert、
- Windows编程之MDI
- hdu 1231 解题报告
- 使用反射创建Bean、Spring中是如何根据类名配置创建Bean实例、Java提供了Class类获取类别的字段和方法,包括构造方法
- android Gridview 点击某个item 改变item的背景(不是点击效果是选中效果)