-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIsBalancedBinaryTree.java
80 lines (78 loc) · 1.83 KB
/
IsBalancedBinaryTree.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
package io.ziheng.codinginterviews;
/**
* 二叉树节点
*/
class TreeNode {
public int val;
public TreeNode left;
public TreeNode right;
public TreeNode(int val) {
this.val = val;
this.left = null;;
this.right = null;;
}
}
/**
* 剑指 Offer 面试题 55-2:判断平衡二叉树
*
* 题目描述:
* 输入一棵二叉树,判断该二叉树是否是平衡二叉树。
*
* 知识点:["树"]
*/
public class IsBalancedBinaryTree {
/**
* 主函数 -> 测试用例
*
* @param args
* @return void
*/
public static void main(String[] args) {
IsBalancedBinaryTree obj = new IsBalancedBinaryTree();
TreeNode t1 = new TreeNode(1);
TreeNode t2 = new TreeNode(2);
TreeNode t3 = new TreeNode(3);
t1.left = t2;
t1.right = t3;
TreeNode root = t1;
TreeNode root2 = new TreeNode(22);
root2.left = t1;
System.out.println(
obj.isBalanced(root)
);
System.out.println(
obj.isBalanced(root2)
);
}
/**
* 剑指 Offer 面试题 55-2:判断平衡二叉树
*
* 时间复杂度:O(n^2)
* 空间复杂度:O(n)
*
* @param root
* @return boolean
*/
public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
if (Math.abs(treeDepth(root.left) - treeDepth(root.right)) > 1) {
return false;
}
return isBalanced(root.left) && isBalanced(root.right);
}
/**
* 计算树的高度。
*
* @param root
* @return int
*/
private int treeDepth(TreeNode root) {
if (root == null) {
return 0;
}
return 1 + Math.max(treeDepth(root.left), treeDepth(root.right));
}
}
/* EOF */