[Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced 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

 1 public int minDepth(TreeNode root) {
 2         if(root==null)
 3             return 0;
 4         if(root.left==null && root.right==null)
 5             return 1;
 6         if(root.right==null)
 7             return minDepth(root.left)+1;
 8         if(root.left==null)
 9             return minDepth(root.right)+1;
10         return Math.min(minDepth(root.right),minDepth(root.left))+1;
11     }

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

 1     public boolean isBalanced(TreeNode root) {
 2         if(height(root)==-1)
 3             return false;
 4         return true;
 5     }
 6     public int height(TreeNode root)
 7     {
 8         if(root==null)
 9             return 0;
10         int left = height(root.left);
11         int right = height(root.right);
12         if(left==-1 || right==-1)
13             return -1;
14         if(Math.abs(left-right)>1)
15             return -1;
16         return Math.max(left,right)+1;
17     }
时间: 2024-08-02 11:03:56

[Leetcode][JAVA] Minimum Depth of Binary Tree && Balanced 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 shortest path from the root node down to the nearest leaf node. 题意: 给定一棵二叉树.返回它的最小高度. 最小高度是指从根节点到近期的叶子节点的最短路径中的节点的数目. 算法分析: * 借助堆 * 类似<Binary Tree Lev

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

[LeetCode]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. 递归和非递归,此提比较简单.广度优先遍历即可.关键之处就在于如何保持访问深度. 下面是4种代码: 1

[Leetcode][Tree][Balanced Binary Tree]

判断一棵树是不是平衡二叉树,之前做过,还有点印象,用一个函数返回树的高度,如果是-1的话,就说明子树不平衡. 1A很开心~ 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 cla

Java for 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 shortest path from the root node down to the nearest leaf node. 解题思路:注意minimum depth最后遍历的那个点,left right都必须为null,JAVA实现如下: public int minDepth(TreeNode roo

[LeetCode] 111. Minimum Depth of Binary Tree Java

题目: 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. 题意及分析:求一棵二叉树的最低高度,即根节点到叶子节点的最短路径.遍历二叉树,然后用一个变量保存遍历到当前节点的最小路径即可,然后每次遇到叶子节点,就判断当前叶节点高度和先

Java [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 shortest path from the root node down to the nearest leaf node. 解题思路: 递归. 代码描述: /** * Definition for a binary tree node. * public class TreeNode { *

33. Minimum Depth of Binary Tree &amp;&amp; Balanced Binary Tree

Minimum Depth of Binary Tree OJ: https://oj.leetcode.com/problems/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

Leetcode 111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree Total Accepted: 114343 Total Submissions:368035 Difficulty: Easy 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 le

leetcode dfs Minimum Depth of Binary Tree

Minimum Depth of Binary Tree Total Accepted: 25609 Total Submissions: 86491My Submissions 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.