Maximum Depth of Binary Tree leetcode java
题目:
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.
题解:
递归解法:
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int leftmax = maxDepth(root.left);
int rightmax = maxDepth(root.right);
return Math.max(leftmax, rightmax)+1;
}
非递归解法,参考codeganker(http://codeganker.blogspot.com/2014/02/maximum-depth-of-binary-tree-leetcode.html)的:
1 public int maxDepth(TreeNode root) {
2 if(root == null)
3 return 0;
4
5 int depth = 0;
6 LinkedList<TreeNode> queue = new LinkedList<TreeNode>();
7 queue.add(root);
8 int curNum = 1; //num of nodes left in current level
9 int nextNum = 0; //num of nodes in next level
while(!queue.isEmpty()){
TreeNode n = queue.poll();
curNum--;
if(n.left!=null){
queue.add(n.left);
nextNum++;
}
if(n.right!=null){
queue.add(n.right);
nextNum++;
}
if(curNum == 0){
curNum = nextNum;
nextNum = 0;
depth++;
}
}
return depth;
}
Maximum Depth of Binary Tree leetcode java的更多相关文章
- Maximum Depth of Binary Tree leetcode
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Minimum Depth of Binary Tree leetcode java
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- [Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度 104. Maximum Depth of Binary Tree 题目描述 给定一个二叉树,找出其最大深度. 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数. 说 ...
- LeetCode:Minimum Depth of Binary Tree,Maximum Depth of Binary Tree
LeetCode:Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth ...
- LeetCode——Maximum Depth of Binary Tree
LeetCode--Maximum Depth of Binary Tree Question Given a binary tree, find its maximum depth. The max ...
- LEETCODE —— binary tree [Same Tree] && [Maximum Depth of Binary Tree]
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- Leetcode | Minimum/Maximum Depth of Binary Tree
Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum depth is the n ...
- 【LeetCode练习题】Maximum Depth of Binary Tree
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the n ...
随机推荐
- perf工具crash的问题
perf抓取时系统crash的情况.找前同事了解到perf工具导致系统crash的一种情况, perf工具默认是使用cycles,这个硬件事件是使用NMI,可能会导致内核错误. 之前文档上的perf命 ...
- 全链路压测平台(Quake)在美团中的实践
背景 在美团的价值观中,以“客户为中心”被放在一个非常重要的位置,所以我们对服务出现故障越来越不能容忍.特别是目前公司业务正在高速增长阶段,每一次故障对公司来说都是一笔非常不小的损失.而整个IT基础设 ...
- date time insert
DATE=`date '+%m/%d/%Y'`TIME=`date '+%H:%M:%S'` sed -i '1i1***** start*****' test.kshsed -i '2i\ REPO ...
- vsftp 虚拟用户+MySQL认证独立家目录
centos7 系统 安装包 yum -y install mariadb vsftpd openssl-devel mysql-devel pam-devel yum -y groupinsta ...
- 机器学习之路:python 字典特征提取器 DictVectorizer
python3 学习使用api 将字典类型数据结构的样本,抽取特征,转化成向量形式 源码git: https://github.com/linyi0604/MachineLearning 代码: fr ...
- play framework系列之打包war
概览 Play framwork 是我们一直在使用的框架,从刚开始的简单使用,乱起八糟的jar包引用,项目组成员之间的下载项目之后的引用问题等,遇到各种问题,我都一一解决,我将在这个系列中奉上解决方案 ...
- BZOJ 1003 物流运输trans dijstra+dp
1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3896 Solved: 1608[Submit] ...
- HashMap和Hashtable的区别--List,Set,Map等接口是否都继承自Map接口--Collection和Collections的区别
面试题: 1.HashMap和Hashtable的区别? HashMap:线程不安全,效率高,键和值都允许null值 Hashtable:线程安全,效率低,键和值都不允许null值 ArrayList ...
- Redis篇
一:下载redis 官网地址:http://redis.io/ 如果系统没有安装make,请查看mysql篇 wget http://download.redis.io/redis-stabl ...
- jquery easyui combobox设置默认选中第一项
combobox的内容是从后台获取的json, js截取: var data = $('#id').combobox('getData'); $("#id ").combobox( ...