-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsolution.ts
More file actions
38 lines (31 loc) · 730 Bytes
/
solution.ts
File metadata and controls
38 lines (31 loc) · 730 Bytes
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
/*
* @lc app=leetcode id=700 lang=javascript
*
* [700] Search in a Binary Search Tree
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
interface TreeNode {
val: number;
left: TreeNode | null;
right: TreeNode | null;
}
/**
* @param {TreeNode} root
* @param {number} val
* @return {TreeNode}
*/
const searchBST = (root: TreeNode | null, val: number): TreeNode | null => {
// * ['76 ms', '81.34 %', '42.2 MB', '6.25 %']
if (root === null) return null;
if (root.val === val) return root;
return searchBST(root.left, val) || searchBST(root.right, val);
};
// @lc code=end
export { searchBST };