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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  4. 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 ...

  5. [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 ...

  6. 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 ...

  7. 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 ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. XdbxAnalysis

    Tree: TXdbxAnalysis; FDataStream: TMemoryStream; {FDataStream:= TMemoryStream.Create;    FDataStream ...

  2. Integer Break

    Given a positive integer n, break it into the sum of at least two positive integers and maximize the ...

  3. oracle 字符转字符串函数

    select cast('addd' as varchar(4)) from dual;

  4. 未能加载文件或程序集EntityFramework, Version=6.0.0.0解决办法

    其他信息: 未能加载文件或程序集"EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934 ...

  5. 【好文要转】HTTP图解(大牛必经之路)

    http://www.cnblogs.com/aylin/p/6221436.html

  6. MAC 安装 Protobuf

    1.确认MAC装有g++.make.vim工具 2.安装make工具使用       brew install make 3.安装protobuf brew install protobuf 4.安装 ...

  7. PE文件头

    pe文件头查看器下载与原文地址: http://www.pc6.com/softview/SoftView_109840.html PE文件入门: PE文件总的来说是由DOS文件头.DOS加载模块.P ...

  8. java中关于正则一些基础使用

    希望能帮到有需要的朋友.-----转载请注明出处. 对于正则处理相关的知识,我一开始是从网上找资料配合使用Java API1.6的一个中文版进行学习,很感谢翻译这个版本的团队(机构)或者个人,很感谢那 ...

  9. Abp项目中的领域模型实体类访问仓储的方法

    首先声明,不推荐使用这种方法.实体访问仓储是不被推荐的! 1.简单粗暴的方法 Abp.Dependency.IocManager.Instance.Resolve>(); 2.绕个弯子的方法 首 ...

  10. [MOSEK] Stupid things when using mosek

    1.2016-8-14 我希望把一个qp问题的代码从conic constraints改为无外加约束,仅适用variable bounds的线性不等式约束 于是原来的约束代码为 if (r == MS ...