Skip to content

Commit b0e961c

Browse files
committed
feat: add 111
1 parent 2070d15 commit b0e961c

File tree

8 files changed

+168
-42
lines changed

8 files changed

+168
-42
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
我如今是一名Android Developer,大学的我曾是一名ACMer,为了不想让算法淡出我的记忆,故重拾LeetCode之Algorithm,语言选择的是Java,题库会一点点完善起来,按简单,中等,困难分类,相应难度下按题号排序,工程代码在[project][project]目录中,相关解题都在[note][note]目录中,欢迎star。
44

5+
如今有机会面试Facebook,新的一轮动力刷起来,即使没进那也至少努力过了,不过Facebook每年都有机肥去面试,所以更要加油了。
6+
57
## Easy
68

79
|#|Title|Tag|

note/110/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ For this problem, a height-balanced binary tree is defined as a binary tree in w
1111

1212
## 思路
1313

14-
题意是判断一棵二叉树是否是高度平衡的,所谓二叉树高度平衡指的是二叉树的每个节点的两棵子树的高度差都不超过1,那么我们只需计算左右子树的高度,判断其高度差是否不超过1即可,如果超过1,就代表其不是高度平衡的,立即返回不是即可。
14+
题意是判断一棵二叉树是否是高度平衡的,所谓二叉树高度平衡指的是二叉树的每个节点的两棵子树的高度差都不超过1,那么我们只需计算左右子树的高度,判断其高度差是否不超过1即可,如果超过1,就代表其不是高度平衡的,立即返回不是即可,我这里用返回`-1`代表不是
1515

1616

1717
``` java

note/111/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# [Balanced Binary Tree][title]
2+
3+
## Description
4+
5+
Given a binary tree, determine if it is height-balanced.
6+
7+
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.
8+
9+
**Tags:** Tree, Depth-first Search
10+
11+
12+
## 思路
13+
14+
题意是判断一棵二叉树是否是高度平衡的,所谓二叉树高度平衡指的是二叉树的每个节点的两棵子树的高度差都不超过1,那么我们只需计算左右子树的高度,判断其高度差是否不超过1即可,如果超过1,就代表其不是高度平衡的,立即返回不是即可,我这里用返回`-1`代表不是。
15+
16+
17+
``` java
18+
/**
19+
* Definition for a binary tree node.
20+
* public class TreeNode {
21+
* int val;
22+
* TreeNode left;
23+
* TreeNode right;
24+
* TreeNode(int x) { val = x; }
25+
* }
26+
*/
27+
class Solution {
28+
public boolean isBalanced(TreeNode root) {
29+
return helper(root) != -1;
30+
}
31+
32+
private int helper(TreeNode node) {
33+
if (node == null) return 0;
34+
int l = helper(node.left);
35+
if (l == -1) return -1;
36+
int r = helper(node.right);
37+
if (r == -1) return -1;
38+
if (Math.abs(l - r) > 1) return -1;
39+
return 1 + Math.max(l, r);
40+
}
41+
}
42+
```
43+
44+
45+
## 结语
46+
47+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
48+
49+
50+
51+
[title]: https://leetcode.com/problems/balanced-binary-tree
52+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/leetcode/.idea/workspace.xml

+81-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.

project/leetcode/src/com/blankj/easy/_110/Solution.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ private int helper(TreeNode node) {
2828

2929
public static void main(String[] args) {
3030
Solution solution = new Solution();
31-
System.out.println(solution.isBalanced(TreeNode.createTestData("[1,2,2,3,3,3,3,4,4,4,4,4,4,null,null,5,5]")));
31+
TreeNode testData = TreeNode.createTestData("[1,2,2,3,3,3,3,4,4,4,4,4,4,null,null,5,5]");
32+
TreeNode.print(testData);
33+
System.out.println(solution.isBalanced(testData));
3234
}
3335
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.blankj.easy._111;
2+
3+
4+
import com.blankj.structure.TreeNode;
5+
6+
/**
7+
* <pre>
8+
* author: Blankj
9+
* blog : http://blankj.com
10+
* time : 2017/10/09
11+
* desc :
12+
* </pre>
13+
*/
14+
public class Solution {
15+
public int minDepth(TreeNode root) {
16+
if (root == null) return 0;
17+
int l = minDepth(root.left);
18+
int r = minDepth(root.right);
19+
if (l == 0 || r == 0) return l + r + 1;
20+
return 1 + Math.min(l, r);
21+
}
22+
23+
public static void main(String[] args) {
24+
Solution solution = new Solution();
25+
TreeNode testData = TreeNode.createTestData("[1,2,2,3,3,3,3,4,4,4,4,4,4,null,null,5,5]");
26+
TreeNode.print(testData);
27+
System.out.println(solution.minDepth(testData));
28+
}
29+
}

0 commit comments

Comments
 (0)