-
-
Notifications
You must be signed in to change notification settings - Fork 7.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0700.Search in a Binary Search
Tree
- Loading branch information
Showing
6 changed files
with
245 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
solution/0700-0799/0700.Search in a Binary Search Tree/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 12 additions & 24 deletions
36
solution/0700-0799/0700.Search in a Binary Search Tree/Solution.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |