forked from rprotsyk-zz/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
search-bst.js
50 lines (47 loc) · 859 Bytes
/
search-bst.js
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
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
var tree = {
left: {
left: {
value: 1
},
right: {
value: 3
},
value: 2
},
right: {
left: {
value: 6
},
right: {
value: 9
},
value: 7
},
value: 4
}
/**
* @param {TreeNode} root
* @param {number} val
* @return {TreeNode}
*/
var searchBST = function(root, val) {
if(!root) {
return null;
}
searchBST(root.left, val);
if(root.value == val) {
//console.log(root);
return root
}
//console.log(root.value, val)
searchBST(root.right, val);
};
console.log(searchBST(tree, 4))