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 ...
随机推荐
- $geoNear
怎么使用mongoose的geoNear 2014-11-26 15:05:20| 分类: mongodb | 标签:mongoose |举报|字号 订阅 下载LOFTER我的照片书 ...
- 3.2 Git 分支 - 分支的新建与合并
分支的新建与合并 现在让我们来看一个简单的分支与合并的例子,实际工作中大体也会用到这样的工作流程: 开发某个网站. 为实现某个新的需求,创建一个分支. 在这个分支上开展工作. 假设此时,你突然接到一个 ...
- NOIP 2013提高组day 1 T 1转圈游戏 快速幂
描述 n 个小伙伴(编号从 0 到 n-1)围坐一圈玩游戏.按照顺时针方向给 n 个位置编号,从0 到 n-1.最初,第 0 号小伙伴在第 0 号位置,第 1 号小伙伴在第 1 号位置,……,依此类推 ...
- C++-bool的值
/////////////////////////////////////////////////////////////////////////////// // // FileName : boo ...
- Eclipse快捷键 10个最有用的快捷键---摘录
55 48 Eclipse中10个最有用的快捷键组合 一个Eclipse骨灰级开发者总结了他认为最有用但又不太为人所知的快捷键组合.通过这些组合可以更加容易的浏览源代码,使得整体的开发效率和质量得到 ...
- CCNA 6.6
sh run (show running-config) enable disable login logout exit end enable password privilege level(g ...
- js中获取项目路径的小插件
//立即执行的js (function() { //获取contextPath var contextPath = getContextPath(); //获取basePath var basePat ...
- Map-Reduce的工作机制
Mapper “Map-Reduce”的思想就是“分而治之” Mapper负责“分”,即把复杂的任务分解为若干个“简单的任务”而执行 “简单的任务”有几个意思:1.数据或计算规模相对于原任务要大大缩小 ...
- iOS APP提交上架最新流程
时隔1年又让我鼓捣iOS,刚接手就是上架,经验值为0的我,虽然内心是拒绝的,但还是要接受这项任务滴!也就是在被拒后重新审核,再改在提交...这样反复的过程中也对上架流程熟悉了好多,写篇帖子 ...
- win10 mac地址修改器
NoVirus Thanks MAC Address Changer NoVirus Thanks MAC Address Changer is yet another simple MAC addr ...