- Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathP53_1.java
More file actions
Latest commit
25 lines (22 loc) · 838 Bytes
/
Copy pathP53_1.java
File metadata and controls
25 lines (22 loc) · 838 Bytes
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
packagech14;
importdatatype.TreeNode;
publicclassP53_1 {
publicintdfs(TreeNodenode) {
// 예외 처리
if (node == null)
return0;
// 재귀 DFS로 왼쪽 리프 노드까지 탐색
intleft = dfs(node.left);
// 재귀 DFS로 오른쪽 리프 노드까지 탐색
intright = dfs(node.right);
// 높이 균형이 아닌 경우 -1 리턴
if (left == -1 || right == -1 || Math.abs(left - right) > 1)
return -1;
// 왼쪽과 오른쪽 중 높은 노드 높이 +1 리턴
returnMath.max(left, right) + 1;
}
publicbooleanisBalanced(TreeNoderoot) {
// 높이 균형이 깨진 경우가 아니라면 true, -1이 리턴되어 높이 균형이 깨진 경우 false 리턴
returndfs(root) != -1;
}
}