forked from cherryljr/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalanced Binary Tree.java
More file actions
105 lines (92 loc) · 3.12 KB
/
Balanced Binary Tree.java
File metadata and controls
105 lines (92 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*
Description
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.
Example
Given binary tree A = {3,9,20,#,#,15,7}, B = {3,#,20,15,7}
A) 3 B) 3
/ \ \
9 20 20
/ \ / \
15 7 15 7
The binary tree A is a height-balanced binary tree, but B is not.
Tags
Divide and Conquer Recursion
*/
/**
* Approach 1: Divide and Conquer
* 需要求解一棵树是否为 平衡树,即要求其所有 子树 都是平衡树。
* 因此我们可以通过 分治 的方法来解决。
* 即如果一个树的 左子树 和 右子树 均为平衡树 并且 左右子树高度差 小于等于1
* 则这棵树为平衡树,否则只要有一个条件不符合,就说明其不是平衡树。
*
* 因此在 分治 的过程中,我们需要两个信息:
* 1. 该子树是否为平衡树; 2. 该子树的高度是多少(用于判断是否为平衡树)
* 然后进行 递归 判断即可。
*/
/**
* Definition of TreeNode:
* public class TreeNode {
* public int val;
* public TreeNode left, right;
* public TreeNode(int val) {
* this.val = val;
* this.left = this.right = null;
* }
* }
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
int leftDepth = maxDepth(root.left);
int rightDepth = maxDepth(root.right);
if (Math.abs(leftDepth - rightDepth) > 1) {
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
}
private int maxDepth(TreeNode node) {
if (node == null) {
return 0;
}
return Math.max(maxDepth(node.left), maxDepth(node.right)) + 1;
}
}
/**
* Approach 2: Divide anc Conquer
* 在 Approach 1 中,我们分析出来了可以利用 分治 的方法解决这道题目。
* 并且 分治 的过程中需要用到 两个 信息。
* 但是考虑到 高度 这个信息的 非负性 这个特点。
* 我们可以利用 -1 这个值来表示其为 非平衡树 这个信息,从而节约空间。
* 其他做法与 Approach 1 相同,具体实现请看代码。
*/
public class Solution {
/**
* @param root: The root of binary tree.
* @return: True if this Binary tree is Balanced, or false.
*/
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
return maxDepth(root) != -1;
}
private int maxDepth(TreeNode node) {
if (node == null) {
return 0;
}
int leftDepth = maxDepth(node.left);
int rightDepth = maxDepth(node.right);
if (leftDepth == -1 || rightDepth == -1 || Math.abs(leftDepth - rightDepth) > 1) {
return -1;
}
return Math.max(leftDepth, rightDepth) + 1;
}
}