-
Notifications
You must be signed in to change notification settings - Fork 2
/
IsBalanced.java
90 lines (75 loc) · 2.31 KB
/
IsBalanced.java
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
package org.offer.case39;
import org.offer.utils.node.BinaryTreeNode;
/**
* 面试题 39
* 题目二 : 判断一颗二叉树是否为平衡的
* Created by tanc on 2017/7/5.
*/
public class IsBalanced {
/**
* 方法一:借鉴求树深度,判断该树是否为平衡二叉树
*/
public static <E> boolean methodOne(BinaryTreeNode<E> root) {
return null != root && isBalanced(root);
}
private static <E> boolean isBalanced(BinaryTreeNode<E> root) {
if (null == root) {
return true;
}
int left = treeDepth(root.left);
int right = treeDepth(root.right);
int diff = left - right;
if (diff > 1 || diff < -1) {
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
}
/**
* 递归求树深度
*/
private static <E> int treeDepth(BinaryTreeNode<E> root) {
if (null == root) {
return 0;
}
int left = treeDepth(root.left);
int right = treeDepth(root.right);
return left > right ? left + 1 : right + 1;
}
/**
* 方法二:使用后续遍历,每个节点添加一个深度属性
*/
public static <E> boolean methodTwo(BinaryTreeNode<E> root) {
if (null == root) {
return false;
}
Node<E> node = new Node<>(root, 0);
return isBalanced(node);
}
private static <E> boolean isBalanced(Node<E> node) {
if (null == node.node) {
node.depth = 0;
return true;
}
Node<E> leftNode = new Node<>(node.node.left, 0);
Node<E> rightNode = new Node<>(node.node.right, 0);
if (isBalanced(leftNode) && isBalanced(rightNode)) {
int diff = leftNode.depth - rightNode.depth;
if (diff <= 1 && diff >= -1) {
node.depth = leftNode.depth > rightNode.depth ? leftNode.depth + 1 : rightNode.depth + 1;
return true;
}
}
return false;
}
/**
* 把 BinaryTreeNode 封装一下,添加一个深度属性
*/
private static class Node<T> {
BinaryTreeNode<T> node;
int depth;
public Node(BinaryTreeNode<T> node, int depth) {
this.node = node;
this.depth = depth;
}
}
}