Skip to content

Commit

Permalink
700_Search_in_a_Binary_Search_Tree
Browse files Browse the repository at this point in the history
  • Loading branch information
qiyuangong committed Dec 22, 2018
1 parent a8744c4 commit f96c512
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ Also, there are open source implementations for basic data structs and algorithm
| 692 | [Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/692_Top_K_Frequent_Words.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/692_Top_K_Frequent_Words.java) | 1. Sort based on frequency and alphabetical order, O(nlgn) and O(n)<br>2. Find top k with Heap, O(nlogk) and O(n) |
| 695 | [Max Area of Island](https://leetcode.com/problems/max-area-of-island/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/695_Max_Area_of_Island.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/695_Max_Area_of_Island.java) | 1. DFS, O(n^2) and O(n)<br>2. BFS, O(n^2) and O(n)|
| 697 | [Degree of an Array](https://leetcode.com/problems/degree-of-an-array/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/697_Degree_of_an_Array.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/697_Degree_of_an_Array.java) | 1. Find degree and value, then find smallest subarray (start and end with this value), O(n) and O(n)<br>2. Go through nums, remember left most pos and right most for each value, O(n) and O(n) |
| 700 | [Search in a Binary Search Tree](https://leetcode.com/problems/search-in-a-binary-search-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/700_Search_in_a_Binary_Search_Tree.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/700_Search_in_a_Binary_Search_Tree.java) | Recursive or iteration, O(logn) |
| 703 | [Kth Largest Element in a Stream](https://leetcode.com/problems/kth-largest-element-in-a-stream/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/703_Kth_Largest_Element_in_a_Stream.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/703_Kth_Largest_Element_in_a_Stream.java) | 1. Sort and insert into right place, O(nlgn) and O(n)<br>2. k largest heap, O(nlogk) and O(n) |
| 706 | [Design HashMap](https://leetcode.com/problems/design-hashmap/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/706_Design_HashMap.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/706_Design_HashMap.java) | Hash implementation, mod is fine. Be careful about key conflict and key remove. |
| 709 | [To Lower Case](https://leetcode.com/problems/to-lower-case/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/709_To_Lower_Case.py) [Java](https://github.com/qiyuangong/leetcode/blob/master/java/709_To_Lower_Case.java) | String processing:<br>1. str.lower() or str.toLowerCase()<br>2. ASCII processing. O(n) and O(1) |
Expand Down
24 changes: 24 additions & 0 deletions java/700_Search_in_a_Binary_Search_Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
/*public TreeNode searchBST(TreeNode root, int val) {
// Recursive
if (root == null) return root;
if (root.val == val) return root;
else return val<root.val? searchBST(root.left,val):searchBST(root.right,val);
}*/
public TreeNode searchBST(TreeNode root, int val) {
// Iteration
while(root != null && root.val != val) {
root = val < root.val ? root.left: root.right;
}
return root;
}
}
33 changes: 33 additions & 0 deletions python/700_Search_in_a_Binary_Search_Tree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution(object):
# def searchBST(self, root, val):
# """
# :type root: TreeNode
# :type val: int
# :rtype: TreeNode
# """
# # Recursive
# if not root:
# return None
# if root.val == val:
# return root
# elif root.val > val:
# return self.searchBST(root.left, val)
# else:
# return self.searchBST(root.right, val)

def searchBST(self, root, val):
while root:
if root.val == val:
return root
elif root.val > val:
root = root.left
else:
root = root.right
return root
13 changes: 11 additions & 2 deletions python/867_Transpose_Matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,14 @@ def transpose(self, A):
for c, val in enumerate(row):
ans[c][r] = val
return ans
#Alternative Solution:
#return zip(*A)
# Alternative Solution:
# return zip(*A)

# def transpose(self, A):
# res = []
# for i in range(len(A[0])):
# temp = []
# for j in range(len(A)):
# temp.append(A[j][i])
# res.append(temp)
# return res

0 comments on commit f96c512

Please sign in to comment.