GeezerVison is showing github.com — 700_Search_in_a_Binary_Search_Tree · Taogal/leetcode-python@f96c512
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: README.md +1Lines changed: 1 addition & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -167,6 +167,7 @@ Also, there are open source implementations for basic data structs and algorithm | 167 | 167 | | 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) | | 168 | 168 | | 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)| | 169 | 169 | | 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) | | | 170 | +| 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) | | 170 | 171 | | 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) | | 171 | 172 | | 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. | | 172 | 173 | | 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) | | |
Collapse file Copy file name to clipboard +24Lines changed: 24 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/** | | 2 | + * Definition for a binary tree node. | | 3 | + * public class TreeNode { | | 4 | + * int val; | | 5 | + * TreeNode left; | | 6 | + * TreeNode right; | | 7 | + * TreeNode(int x) { val = x; } | | 8 | + * } | | 9 | + */ | | 10 | +class Solution { | | 11 | + /*public TreeNode searchBST(TreeNode root, int val) { | | 12 | + // Recursive | | 13 | + if (root == null) return root; | | 14 | + if (root.val == val) return root; | | 15 | + else return val<root.val? searchBST(root.left,val):searchBST(root.right,val); | | 16 | + }*/ | | 17 | + public TreeNode searchBST(TreeNode root, int val) { | | 18 | + // Iteration | | 19 | + while(root != null && root.val != val) { | | 20 | + root = val < root.val ? root.left: root.right; | | 21 | + } | | 22 | + return root; | | 23 | + } | | 24 | +} |
Collapse file Copy file name to clipboard +33Lines changed: 33 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Definition for a binary tree node. | | 2 | +# class TreeNode(object): | | 3 | +# def __init__(self, x): | | 4 | +# self.val = x | | 5 | +# self.left = None | | 6 | +# self.right = None | | 7 | + | | 8 | +class Solution(object): | | 9 | + # def searchBST(self, root, val): | | 10 | + # """ | | 11 | + # :type root: TreeNode | | 12 | + # :type val: int | | 13 | + # :rtype: TreeNode | | 14 | + # """ | | 15 | + # # Recursive | | 16 | + # if not root: | | 17 | + # return None | | 18 | + # if root.val == val: | | 19 | + # return root | | 20 | + # elif root.val > val: | | 21 | + # return self.searchBST(root.left, val) | | 22 | + # else: | | 23 | + # return self.searchBST(root.right, val) | | 24 | + | | 25 | + def searchBST(self, root, val): | | 26 | + while root: | | 27 | + if root.val == val: | | 28 | + return root | | 29 | + elif root.val > val: | | 30 | + root = root.left | | 31 | + else: | | 32 | + root = root.right | | 33 | + return root |
Collapse file Copy file name to clipboardExpand all lines: python/867_Transpose_Matrix.py +11-2Lines changed: 11 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -10,5 +10,14 @@ def transpose(self, A): | 10 | 10 | for c, val in enumerate(row): | 11 | 11 | ans[c][r] = val | 12 | 12 | return ans | 13 | | - #Alternative Solution: | 14 | | - #return zip(*A) | | 13 | + # Alternative Solution: | | 14 | + # return zip(*A) | | 15 | + | | 16 | + # def transpose(self, A): | | 17 | + # res = [] | | 18 | + # for i in range(len(A[0])): | | 19 | + # temp = [] | | 20 | + # for j in range(len(A)): | | 21 | + # temp.append(A[j][i]) | | 22 | + # res.append(temp) | | 23 | + # return res |
|