leedcode做题总结,题目Maximum Depth of Binary Tree-------2012/09/29
来源:互联网 发布:自动微信加好友软件 编辑:程序博客网 时间:2024/06/06 01:53
测树高,用的是递归的方法,非常简单,不过我一开始使用if判断right和left的大小导致了超时,后来直接用Math.max()通过。
记得数据结构考试曾经最后一道大题也是测树高,但是要求不能用递归,结果当时没写出来。。。现在想了想可以用队列进行广度优先历遍,用m记录队列中剩余本层的节点数,没出队列一个m--,同时用n记录下一层节点数,每进队列一个n++;当m==0本层访问完时,层数(树高)+1同时m=n;n=0开始下一层历遍
public int maxDepth(TreeNode root) { if(root==null)return 0; return Math.max(maxDepth(root.left),maxDepth(root.right))+1; }
0 0
- leedcode做题总结,题目Maximum Depth of Binary Tree-------2012/09/29
- leedcode做题总结,题目Minimum Depth of Binary Tree 2012/10/09
- [leedcode oj 104]Maximum Depth of Binary Tree
- leedcode做题总结,题目Binary Tree Level Order TraversalI/II 2012/09/28
- leedcode做题总结,题目Binary Tree Zigzag Level Order Traversal 2012/09/28
- leedcode做题总结,题目Same Tree 2012/09/03
- leedcode做题总结,题目Symmetric Tree 2012/09/23
- [LeetCode]Maximum Depth of Binary Tree做题笔记
- leedcode做题总结,题目Balanced Binary Tree 2012/10/08
- leedcode做题总结,题目Flatten Binary Tree to Linked List 2012/10/14
- CODE 29: Maximum Depth of Binary Tree
- 第二题 Maximum Depth of Binary Tree
- LeetCode题目笔记(二) -- Maximum Depth of Binary Tree
- Easy-题目3:104. Maximum Depth of Binary Tree
- LeetCode算法题目:Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- Maximum Depth of Binary Tree
- UNIX网络编程——send与recv函数详解
- mindmanager2012打开文件出现runtime error r6025 解决方案
- Hibernate 实体基本增删查改操作
- POJ3740 Easy Finding(Dancing Links)
- 自定义ActionBar标题与菜单中的文字样式
- leedcode做题总结,题目Maximum Depth of Binary Tree-------2012/09/29
- ASP.NET MVC Model验证(三)
- 发到你就是房价就
- linux 命令 cat 详解
- 如何在Linux下安装Hadoop
- 如何在(Linux mint)ubuntu中安装中文输入法
- 如同雨后的认同公司饿饿地方染色
- Programmer’s Toolbox Part 3: Consistent Hashing
- 使用 jsPlumb 绘制拓扑图 —— 异步加载与绘制的实现