Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
25 lines (22 loc) · 838 Bytes

File metadata and controls

25 lines (22 loc) · 838 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package ch14;
import datatype.TreeNode;
public class P53_1 {
public int dfs(TreeNode node) {
// 예외 처리
if (node == null)
return 0;
// 재귀 DFS로 왼쪽 리프 노드까지 탐색
int left = dfs(node.left);
// 재귀 DFS로 오른쪽 리프 노드까지 탐색
int right = dfs(node.right);
// 높이 균형이 아닌 경우 -1 리턴
if (left == -1 || right == -1 || Math.abs(left - right) > 1)
return -1;
// 왼쪽과 오른쪽 중 높은 노드 높이 +1 리턴
return Math.max(left, right) + 1;
}
public boolean isBalanced(TreeNode root) {
// 높이 균형이 깨진 경우가 아니라면 true, -1이 리턴되어 높이 균형이 깨진 경우 false 리턴
return dfs(root) != -1;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.