forked from onlybooks/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP57_2.java
More file actions
19 lines (16 loc) · 776 Bytes
/
P57_2.java
File metadata and controls
19 lines (16 loc) · 776 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package ch14;
import datatype.TreeNode;
public class P57_2 {
public int rangeSumBST(TreeNode root, int low, int high) {
// 예외 처리
if (root == null) return 0;
// 노드 값이 high보다 크면 더 작아야 하므로 BST 왼쪽 노드 탐색
if (root.val > high)
return rangeSumBST(root.left, low, high);
// 노드 값이 low보다 작으면 더 커야 하므로 BST 오른쪽 노드 탐색
if (root.val < low)
return rangeSumBST(root.right, low, high);
// 여기까지 도달했다면 노드 값이 low와 high 범위 내에 있으므로, 모든 결과를 합산하여 리턴
return root.val + rangeSumBST(root.left, low, high) + rangeSumBST(root.right, low, high);
}
}