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. VPN服务器是什么?

    可能很多人听到VPN的第一感觉是它是一个FQ的工具,然而并不是酱紫的. 虚拟专用网(Virtual Private Network,简称VPN),是一种常用于连接中.大型企业或团体与团体间的私人网络的 ...

  2. 【POJ2778】DNA Sequence(AC自动机,DP)

    题意: 生物课上我们学到,DNA序列中只有A, C, T和G四种片段. 经科学发现,DNA序列中,包含某些片段会产生不好的基因,如片段"ATC"是不好片段,则"AGATC ...

  3. Mob.com 短信验证的简单使用

    1.环境配置 http://wiki.sharesdk.cn/android-短信sdk集成文档/ a.sdk下载 http://www.mob.com/#/downloadDetail/SMS/an ...

  4. 修改Arduino串口缓冲区大小(转)

    本帖节选自<Arduino程序设计基础>第二版5.1.6串口缓冲区       在之前的示例程序中,我们都是采用人工输入测试数据的方式检验程序效果,Arduino每接收到一次数据,就会将数 ...

  5. Android WIFI 分析(二)

    本文介绍Wifi 分析线路二:在Setting中打开WiFi功能.扫描网络以及连接网络的流程. WifiSettings 无线网络设置界面 WifiEnabler 相当于无线网络设置开关 WifiDi ...

  6. (转)如何在一台电脑上开启多个tomcat 和配置让系统识别哪个具体的tomcat

    大家基本上都只在一台电脑上面启动一个Tomcat,而启动多个Tomcat会提示报错等相关故障.而假如调试负载均衡及集群的时候,需要在一台电脑上面开启多个Tomcat,那么怎么开启呢? 方法/步骤 首先 ...

  7. mysql在linux下不区分大小写

    1.先停止mysql service mysql stop 2.如果用rpm直接安装的mysql,路径在:/usr/下,查找my.cnf. 3.在[mysqld]下添加: lower_case_tab ...

  8. Knockout.Js官网学习(value绑定)

    前言 value绑定是关联DOM元素的值到view model的属性上.主要是用在表单控件<input>,<select>和<textarea>上. 当用户编辑表单 ...

  9. cmd运行的程序的工作目录

    如图所示,cmd通过输入自己编写的程序的实际路径,或者将程序放在环境变量中然后在cmd中执行,用start执行,该程序运行时的工作目录都是cmd当前所在目录:在cmd中输入该程序的快捷方式执行该程序, ...

  10. [python] 创建临时文件-tempfile模块

    This module generates temporary files and directories. It works on all supported platforms.In versio ...