-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution669.java
More file actions
61 lines (57 loc) · 1.53 KB
/
Copy pathSolution669.java
File metadata and controls
61 lines (57 loc) · 1.53 KB
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution669 {
private int min_num;
private int max_num;
public TreeNode trimBST(TreeNode root, int low, int high) {
min_num = low - 1;
max_num = high + 1;
inorder(root);
TreeNode res = new TreeNode(root.val, root.left, root.right);
return res;
}
public void inorder(TreeNode root, TreeNode res) {
if (root == null) return;
// 访问左子树
inorder(root.left, res.left);
// 当前节点
if (root.val <= min_num) {
res = res.right;
}
if (root.val >= max_num) {
res = res.left;
}
// 访问右子树
inorder(root.right, res.right);
}
}
class Solution {
public TreeNode trimBST(TreeNode root, int low, int high) {
return dfs(root, low, high);
}
public TreeNode dfs(TreeNode root, int low, int high) {
if (root == null) return root;
root.left = dfs(root.left, low, high);
root.right = dfs(root.right, low, high);
if (root.val > high) {
root = root.left;
}
else if (root.val < low) {
root = root.right;
}
return root;
}
}