forked from YuSkyBlue/java-algorithm-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP57_1.java
More file actions
21 lines (17 loc) · 583 Bytes
/
P57_1.java
File metadata and controls
21 lines (17 loc) · 583 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package ch14;
import datatype.TreeNode;
public class P57_1 {
public int rangeSumBST(TreeNode root, int low, int high) {
// 예외 처리
if (root == null) return 0;
// 결과 변수
int result = 0;
// 현재 노드의 값이 low와 high 사이에 있다면 결과에 추가
if (low <= root.val && root.val <= high)
result = root.val;
// 자식 노드 재귀 DFS 진행
result += rangeSumBST(root.left, low, high);
result += rangeSumBST(root.right, low, high);
return result;
}
}