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 ...
随机推荐
- Java异常--读书笔记
1. Java将异常分为两种:Checked异常和Runtime异常,Java认为Checked异常都是可以在编译阶段被处理的异常,所以强制程序处理所有的Checked异常:Runtime异常则无需处 ...
- ios 8.4 Xcode6.4 设置LaunchImage图片
Step1 1.点击Image.xcassets 进入图片管理,然后右击,弹出"New Launch Image" 2.如图,右侧的勾选可以让你选择是否要对ipad,横屏,竖屏,以 ...
- Quartz之CronExpression
一 Cron Expression 的格式 Quartz的cronExpression见官网文档说明: http://www.docjar.com/docs/api/org/quartz/CronEx ...
- Ubuntu 14.10 下grep命令详解
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- C++数据结构之List--线性实现
List(表)类似于队列,不同于队列的是,list可以随机读取/修改/插入某一position,通过position这一位置信息就可以直接修改相应位置的元素.实现方式和队列的类似,多了个positio ...
- 一道面试题:按照其描述要求用java语言实现快速排序
回来想了想,写出了如下的程序: /** * 一道面试题,按照其描述要求进行快速排序(英文的,希望理解是对的..) * 要求:和一般的快速排序算法不同的是,它不是依次交换pivot和左右元素节点(交换2 ...
- iOS开发之修改动画对象的元素属性
步骤:1.使用single view application创建新的项目 2.在.h文件中使用UIimageview创建两个图片实例对象,使用UIDynamicAnimator创建动画对象 3.在.m ...
- 爬虫学习----获取cookie
http://blog.csdn.net/samxx8/article/details/21535901 1.获取cookie import urllib import http.cookiejarc ...
- BZOJ 2296 随机种子
RT. #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> ...
- http://182.92.241.20/mypro/login 偶的点金项目细化分包管理平台即将上线!!
http://182.92.241.20/mypro/login 偶的点金项目细化分包管理平台即将上线!! 敬请期待! 项目外包利器 项目管理利器