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
33 lines (28 loc) · 1.13 KB

File metadata and controls

33 lines (28 loc) · 1.13 KB
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
26
27
28
29
30
31
32
33
package ch14;
import datatype.TreeNode;
import java.util.ArrayDeque;
import java.util.Deque;
public class P57_3 {
public int rangeSumBST(TreeNode root, int low, int high) {
// 반복 구조 DFS 구현을 위한 스택 선언
Deque<TreeNode> stack = new ArrayDeque<>();
// 스택에 루트부터 삽입
stack.push(root);
// 결과 변수 선언
int result = 0;
while (!stack.isEmpty()) {
// 스택에서 노드 추출
TreeNode node = stack.pop();
// 노드 값이 low보다 크면 더 작아도 되므로 왼쪽 자식 노드 스택에 삽입
if (node.val > low)
if (node.left != null) stack.push(node.left);
// 노드 값이 high보다 작으면 더 커도 되므로 오른쪽 자식 노드 스택에 삽입
if (node.val < high)
if (node.right != null) stack.push(node.right);
// 노드 값이 low보다 크고 high보다 작으면 결과에 추가
if (low <= node.val && node.val <= high)
result += node.val;
}
return result;
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.