Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0700.Search in a Binary Search
Browse files Browse the repository at this point in the history
Tree
  • Loading branch information
yanglbme committed Aug 6, 2021
1 parent 87b4bfe commit bd1bf9a
Show file tree
Hide file tree
Showing 6 changed files with 245 additions and 53 deletions.
95 changes: 93 additions & 2 deletions solution/0700-0799/0700.Search in a Binary Search Tree/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@

<p>在上述示例中,如果要找的值是 <code>5</code>,但因为没有节点值为 <code>5</code>,我们应该返回 <code>NULL</code>。</p>


## 解法

<!-- 这里可写通用的实现逻辑 -->
Expand All @@ -44,15 +43,107 @@
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if root is None:
return None
if root.val == val:
return root
if root.val < val:
return self.searchBST(root.right, val)
return self.searchBST(root.left, val)
```

### **Java**

<!-- 这里可写当前语言的特殊实现逻辑 -->

```java
/**
* 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 Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) {
return null;
}
if (root.val == val) {
return root;
}
if (root.val < val) {
return searchBST(root.right, val);
}
return searchBST(root.left, val);
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == nullptr) return nullptr;
if (root->val == val) return root;
if (root->val < val) return searchBST(root->right, val);
return searchBST(root->left, val);
}
};
```
### **Go**
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return nil
}
if root.Val == val {
return root
}
if root.Val < val {
return searchBST(root.Right, val)
}
return searchBST(root.Left, val)
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,112 @@
<li><code>1 &lt;= val &lt;= 10<sup>7</sup></code></li>
</ul>


## Solutions

<!-- tabs:start -->

### **Python3**

```python

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if root is None:
return None
if root.val == val:
return root
if root.val < val:
return self.searchBST(root.right, val)
return self.searchBST(root.left, val)
```

### **Java**

```java
/**
* 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 Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) {
return null;
}
if (root.val == val) {
return root;
}
if (root.val < val) {
return searchBST(root.right, val);
}
return searchBST(root.left, val);
}
}
```

### **C++**

```cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* searchBST(TreeNode* root, int val) {
if (root == nullptr) return nullptr;
if (root->val == val) return root;
if (root->val < val) return searchBST(root->right, val);
return searchBST(root->left, val);
}
};
```
### **Go**
```go
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return nil
}
if root.Val == val {
return root
}
if root.Val < val {
return searchBST(root.Right, val)
}
return searchBST(root.Left, val)
}
```

### **...**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
public:
TreeNode* searchBST(TreeNode* root, int val) {

TreeNode* temp = root;

while( temp != NULL ){
if( temp->val == val ){
return temp;
}
else if( val < temp->val ){
temp = temp->left;
}
else{
temp = temp->right;
}
}

return NULL;
if (root == nullptr) return nullptr;
if (root->val == val) return root;
if (root->val < val) return searchBST(root->right, val);
return searchBST(root->left, val);
}
};
};
20 changes: 20 additions & 0 deletions solution/0700-0799/0700.Search in a Binary Search Tree/Solution.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func searchBST(root *TreeNode, val int) *TreeNode {
if root == nil {
return nil
}
if root.Val == val {
return root
}
if root.Val < val {
return searchBST(root.Right, val)
}
return searchBST(root.Left, val)
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,26 @@
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public TreeNode searchBST(TreeNode root, int val) {
if (root == null) return null;
if (root.val == val) return root;
if (root.val < val) return searchBST(root.right, val);
else return searchBST(root.left, val);
if (root == null) {
return null;
}
if (root.val == val) {
return root;
}
if (root.val < val) {
return searchBST(root.right, val);
}
return searchBST(root.left, val);
}
}
}
36 changes: 12 additions & 24 deletions solution/0700-0799/0700.Search in a Binary Search Tree/Solution.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def searchBST(self, root, val):
"""
:type root: TreeNode
:type val: int
:rtype: TreeNode
"""

temp = root

while temp != None :

if temp.val == val :
return temp
elif val < temp.val :
temp = temp.left
else:
temp = temp.right

return None
def searchBST(self, root: TreeNode, val: int) -> TreeNode:
if root is None:
return None
if root.val == val:
return root
if root.val < val:
return self.searchBST(root.right, val)
return self.searchBST(root.left, val)

0 comments on commit bd1bf9a

Please sign in to comment.