[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 number of nodes along the shortest path from the root node down to the nearest leaf node.
比较左子树和右子树的深度,取小的那个返回上来,并+1。
需要注意的是如果没有左子树或右子树。那么无条件取存在的那一边子树深度,并+1.
如果左子树和右子树都没有,那么就是叶子节点,返回深度1.
如果root自身为null,返回0
public int minDepth(TreeNode root) {
if(root==null)
return 0;
if(root.left==null && root.right==null)
return 1;
if(root.right==null)
return minDepth(root.left)+1;
if(root.left==null)
return minDepth(root.right)+1;
return Math.min(minDepth(root.right),minDepth(root.left))+1;
}
Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
新定义一个函数,表示树的高度,如果是平衡树的话。不是平衡树则置高度为-1. 运用分治法递归,最后得到结果为-1则不是平衡的。基本情况,root==null则高度为0
public boolean isBalanced(TreeNode root) {
if(height(root)==-1)
return false;
return true;
}
public int height(TreeNode root)
{
if(root==null)
return 0;
int left = height(root.left);
int right = height(root.right);
if(left==-1 || right==-1)
return -1;
if(Math.abs(left-right)>1)
return -1;
return Math.max(left,right)+1;
}
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.
求最大深度方法也是一样,更简单了:
public int maxDepth(TreeNode root) {
if(root==null)
return 0;
int left = maxDepth(root.left);
int right = maxDepth(root.right);
return (left>right?(left+1):(right+1));
}
[Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree的更多相关文章
- [LeetCode][Java] Minimum Depth of Binary Tree
题目: Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the ...
- [Leetcode][JAVA] Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 33. Minimum Depth of Binary Tree && Balanced Binary Tree && Maximum Depth of Binary Tree
Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/ Give ...
- [LeetCode] 111. Minimum Depth of Binary Tree 二叉树的最小深度
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 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 | 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#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree
The problem 1: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes ...
- LeetCode 104. Maximum Depth of Binary Tree二叉树的最大深度 C++/Java
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
随机推荐
- UML(统一建模语言)
需求分析阶段 用例图 定义:用例图并不是用来描述用例的.用例图的主要作用是:直观地描述系统对外提供的功能. 用例图的三个要素:角色.系统.用例 用例图的关系: 角色和用例的关系:有关和无关 用例和用例 ...
- 修改ubuntu中usr文件夹的权限后,sudo后出现sudo:must be setuid root问题的解决方案
无意之间,使用sudo chmod -R 777 /usr命令修改了usr文件的所有者,导致sudo:must be setuid root问题的出现,即sudo命令无法使用.网上介绍的方法差不多都相 ...
- Android学习---ListView的点击事件,simpleAdapter和arrayadapter,SimpleCursoAdapter的原理和使用
如题,本文将介绍 listview的点击事件,simpleAdapter和arrayadapter的原理和使用. 1.ListView的注册点击事件 //注册点击事件 personListView.s ...
- XUtils——GET请求
//HttpUtils实例化对象 HttpUtils http = new HttpUtils(); /* *发送请求send(HttpMethod ...
- 分享自制的13套 JQuery Mobile 界面主题(追加4套新款)
15套整合在一起的,其中2套官方+13套自制,款款精致,方便移动开发. 字体默认为微软雅黑. 适配于 JQuery Mobile 1.4.3 下载地址:http://files.cnblogs.com ...
- git & scp
git & scp command : git & scp git git 提交 git checkout/pull =====[在提交前校验远程是否有冲突] git add [< ...
- 文件上传——servlet实现
自己对照别的博主的博客实现的,记录用. 整个上传的结构如下: 上传的页面:unload.jsp <%@ page language="java" import="j ...
- three.js 根据png生成heightmap
Three.js: render real world terrain from heightmap using open data By jos.dirksen on Tue, 07/17/2012 ...
- postgreSQL 统计语句
pg_stat_statements 是 postgresql 的一个扩展,用来统计查询语句,类似于 mysql 的 慢查询. 安装二进制文件 有些发行版可能没有附带这个扩展,则需要用户自己安装, 本 ...
- 动态加载jar包中的类(方式一)
嘛, 直接上代码 public static class TestClassLoader extends ClassLoader { @Override protected Class<?> ...