104. Maximum Depth of Binary Tree
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.
代码如下:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
Stack<Integer> stack=new<Integer>Stack(); int depth=0;
if(root==null)
return 0;
if(root.left==null&&root.right==null)
return 1; if(root.left!=null)
{
depth=1+maxDepth(root.left); if(stack.empty()||stack.peek()<depth)
stack.push(depth);
} if(root.right!=null)
{
depth=1+maxDepth(root.right);
if(stack.empty()||stack.peek()<depth)
stack.push(depth);
} return stack.peek();
}
}
104. Maximum Depth of Binary Tree的更多相关文章
- 104. Maximum Depth of Binary Tree(C++)
104. Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- LeetCode 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
- [LeetCode] 104. Maximum Depth of Binary Tree 二叉树的最大深度
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 【LeetCode】104. Maximum Depth of Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:BFS 方法二:DFS 参考资料 日期 题目 ...
- [刷题] 104 Maximum Depth of Binary Tree
要求 求一棵二叉树的最高深度 思路 递归地求左右子树的最高深度 实现 1 Definition for a binary tree node. 2 struct TreeNode { 3 int va ...
- LeetCode 104. Maximum Depth of Binary Tree
Problem: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along ...
随机推荐
- routeProvider
In a previous post about testing I mentioned that route resolves can make authoring unit tests for a ...
- [转载]Android View.onMeasure方法的理解
2013-12-18 10:56:28 转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure( ...
- python 字典排序 关于sort()、reversed()、sorted()
一.Python的排序 1.reversed() 这个很好理解,reversed英文意思就是:adj. 颠倒的:相反的:(判决等)撤销的 print list(reversed(['dream','a ...
- java的动态绑定和静态绑定
首先是方法的参数是父类对象,传入子类对象是否可行然后引出Parent p = new Children();这句代码不是很理解,google的过程中引出向上转型要理解向上转型又引出了动态绑定从动态绑定 ...
- (转载)Htmlparser Filter 简要归纳
1 . 逻辑关系:与或非 AndFilter() Creates a new instance of an AndFilter. AndFilter(NodeFilter[] pr ...
- JVM-内存分配与回收策略
简单介绍一下Java技术体系下的Java虚拟机内存分配与回收策略. 1.对象优先在Eden分配 大多数情况下,对象在新生代Eden区中分分配.当Eden区已没有足够空间进行分配时,虚拟机将发起一次 ...
- 关押罪犯(noip2010)
解法: (1)搜索(30分) (2)二分(此题属于最大值最小问题) (3)贪心+并查集 下面着重说一下“贪心+并查集” 因为有A.B两座监狱,每个犯人不是在A,就是在B监狱. 至于每个犯人在那个监狱, ...
- asp中cookie欺骗/注入原理与防范
一直以来sql注入被广泛关注,也有专门的防注系统代码.发现,如果代码不严谨也会有cookie欺骗/注入的情况.原来, 防注入系统没有注意到 Cookies 的问题!这里以ASP为例,分析一下cook ...
- hdu 2044
ps:好吧,WA了两次,第一次注意到要用long long了...但是printf那里给忘了...又WA.. 代码:#include "stdio.h"long long dp[5 ...
- Java中的blank final
Java allows the creation of blank finals, which are fields that are declared as final but are not gi ...