-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeetCode-270-Closest-Binary-Search-Tree-Value.java
128 lines (104 loc) · 3.43 KB
/
LeetCode-270-Closest-Binary-Search-Tree-Value.java
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
LeetCode: https://leetcode.com/problems/closest-binary-search-tree-value/
LintCode:
JiuZhang:
ProgramCreek:
Analysis:
[1], 4.428571 -> 1
1.Preorder traversal
Time O(N)
2.
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
//1.Iterative (Level Order Traversal)
// public int closestValue(TreeNode root, double target) {
// if(root == null) return -1;
// int val = root.val;
// Queue<TreeNode> queue = new LinkedList<TreeNode>();
// queue.offer(root);
// while(!queue.isEmpty()){
// TreeNode curr = queue.poll();
// if(Math.abs(target - val) > Math.abs(target - curr.val)){
// val = curr.val;
// }
// if(curr.left != null)queue.offer(curr.left);
// if(curr.right != null)queue.offer(curr.right);
// }
// return val;
// }
// 2.DFS Recursive (Pre-ordder Traversal)
int val = 0;
public int closestValue(TreeNode root, double target) {
if(root == null) return -1;
val = root.val; //init val
closest(root, target);
return val;
}
private void closest(TreeNode root, double target){
if(root == null) return;
if(Math.abs(target - val) > Math.abs(target - root.val)){
val = root.val;
}
closest(root.left, target);
closest(root.right, target);
}
// 3.Iterative
public int closestValue(TreeNode root, double target) {
int val = root.val;
while (root != null) {
if (Math.abs(root.val - target) < Math.abs(val - target)) {
// find a new cloest value
val = root.val;
}
if (root.val > target) {
root = root.left;
} else {
root = root.right;
}
}
return val;
}
// 1. BFS (Inorder Traversal)
// public int closestValue(TreeNode root, double target) {
// Stack<TreeNode> stack = new Stack<>();
// TreeNode curr = root;
// double min = Double.MAX_VALUE;
// int minValue = Integer.MAX_VALUE;
// while (curr != null || !stack.isEmpty()) {
// if (curr != null) {
// stack.push(curr);
// curr = curr.left;
// } else {
// curr = stack.pop();
// if (Math.abs(curr.val - target) < min) {
// // find a new smaller node
// min = Math.abs(curr.val - target);
// minValue = curr.val;
// }
// curr = curr.right;
// }
// }
// return minValue;
// }
// 2.DFS (Inorder Traversal)
int minValue = Integer.MAX_VALUE;
double minDiff = Double.MAX_VALUE;
public int closestValue(TreeNode root, double target) {
if (root.left != null) closestValue(root.left, target);
if (Math.abs(root.val - target) < minDiff) {
minDiff = Math.abs(root.val - target);
minValue = root.val;
}
if (root.right != null) closestValue(root.right, target);
return minValue;
}
}