Skip to content

Commit ecd6b56

Browse files
committed
feat: add 110
1 parent d653dda commit ecd6b56

File tree

7 files changed

+253
-89
lines changed

7 files changed

+253
-89
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
|101|[Symmetric Tree][101]|Tree, Depth-first Search, Breadth-first Search|
3131
|104|[Maximum Depth of Binary Tree][104]|Tree, Depth-first Search|
3232
|107|[Binary Tree Level Order Traversal II][107]|Tree, Breadth-first Search|
33-
|108|[Binary Tree Level Order Traversal II][107]|Tree, Depth-first Search|
33+
|108|[Convert Sorted Array to Binary Search Tree][108]|Tree, Depth-first Search|
34+
|110|[Balanced Binary Tree][110]|Tree, Depth-first Search|
3435

3536

3637
## Medium

note/101/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class Solution {
5151
return root == null || isSymmetricHelper(root.left, root.right);
5252
}
5353

54-
public boolean isSymmetricHelper(TreeNode left, TreeNode right) {
54+
public boolean helper(TreeNode left, TreeNode right) {
5555
if (left == null || right == null) return left == right;
5656
if (left.val != right.val) return false;
5757
return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);

note/110/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。
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

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

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Solution {
1818
// return root == null || isSymmetricHelper(root.left, root.right);
1919
// }
2020
//
21-
// private boolean isSymmetricHelper(TreeNode left, TreeNode right) {
21+
// private boolean helper(TreeNode left, TreeNode right) {
2222
// if (left == null || right == null) return left == right;
2323
// if (left.val != right.val) return false;
2424
// return isSymmetricHelper(left.left, right.right) && isSymmetricHelper(left.right, right.left);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.blankj.easy._110;
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 boolean isBalanced(TreeNode root) {
16+
return helper(root) != -1;
17+
}
18+
19+
private int helper(TreeNode node) {
20+
if (node == null) return 0;
21+
int l = helper(node.left);
22+
if (l == -1) return -1;
23+
int r = helper(node.right);
24+
if (r == -1) return -1;
25+
if (Math.abs(l - r) > 1) return -1;
26+
return 1 + Math.max(l, r);
27+
}
28+
29+
public static void main(String[] args) {
30+
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]")));
32+
}
33+
}

0 commit comments

Comments
 (0)