From f96c51209837446033a20b5fd29f39e8e8fc3399 Mon Sep 17 00:00:00 2001 From: Qiyuan Gong Date: Sat, 22 Dec 2018 22:53:40 +0800 Subject: [PATCH] 700_Search_in_a_Binary_Search_Tree --- README.md | 1 + java/700_Search_in_a_Binary_Search_Tree.java | 24 ++++++++++++++ python/700_Search_in_a_Binary_Search_Tree.py | 33 ++++++++++++++++++++ python/867_Transpose_Matrix.py | 13 ++++++-- 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 java/700_Search_in_a_Binary_Search_Tree.java create mode 100644 python/700_Search_in_a_Binary_Search_Tree.py diff --git a/README.md b/README.md index b617b0a..c3a98f1 100644 --- a/README.md +++ b/README.md @@ -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)
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)
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)
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)
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:
1. str.lower() or str.toLowerCase()
2. ASCII processing. O(n) and O(1) | diff --git a/java/700_Search_in_a_Binary_Search_Tree.java b/java/700_Search_in_a_Binary_Search_Tree.java new file mode 100644 index 0000000..0389652 --- /dev/null +++ b/java/700_Search_in_a_Binary_Search_Tree.java @@ -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 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 diff --git a/python/867_Transpose_Matrix.py b/python/867_Transpose_Matrix.py index 00c3b23..61b3ded 100644 --- a/python/867_Transpose_Matrix.py +++ b/python/867_Transpose_Matrix.py @@ -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