Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3119a42

Browse files
committedOct 6, 2021
Add solution #700
1 parent c21dd0c commit 3119a42

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
648|[Replace Words](./0648-replace-words.js)|Medium|
9090
653|[Two Sum IV - Input is a BST](./0653-two-sum-iv---input-is-a-bst.js)|Easy|
9191
686|[Repeated String Match](./0686-repeated-string-match.js)|Easy|
92+
700|[Search in a Binary Search Tree](./0700-search-in-a-binary-search-tree.js)|Easy|
9293
701|[Insert into a Binary Search Tree](./0701-insert-into-a-binary-search-tree.js)|Medium|
9394
713|[Subarray Product Less Than K](./0713-subarray-product-less-than-k.js)|Medium|
9495
722|[Remove Comments](./0722-remove-comments.js)|Medium|
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* 700. Search in a Binary Search Tree
3+
* https://leetcode.com/problems/search-in-a-binary-search-tree/
4+
* Difficulty: Easy
5+
*
6+
* You are given the root of a binary search tree (BST) and an integer val.
7+
*
8+
* Find the node in the BST that the node's value equals val and return the
9+
* subtree rooted with that node. If such a node does not exist, return null.
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* function TreeNode(val, left, right) {
15+
* this.val = (val===undefined ? 0 : val)
16+
* this.left = (left===undefined ? null : left)
17+
* this.right = (right===undefined ? null : right)
18+
* }
19+
*/
20+
/**
21+
* @param {TreeNode} root
22+
* @param {number} val
23+
* @return {TreeNode}
24+
*/
25+
var searchBST = function(root, val) {
26+
while (root) {
27+
if (root.val === val) {
28+
return root;
29+
}
30+
root = root.val > val ? root.left : root.right;
31+
}
32+
33+
return null;
34+
};

0 commit comments

Comments
 (0)
Please sign in to comment.