From 78c1d03d6fc67fa7b502b7beab7e88151b0f3367 Mon Sep 17 00:00:00 2001 From: mayuezhu Date: Thu, 21 Jan 2016 18:58:55 -0800 Subject: [PATCH] HM2 --- Yuezhu Ma/HM2/101. Symmetric Tree.java | 29 ++++++++ ...t Binary Tree from Preorder and Inord.java | 44 +++++++++++ ...t Binary Tree from Postorder and Inor.java | 37 ++++++++++ ...rt Sorted Array to Binary Search Tree.java | 37 ++++++++++ ...ommon Ancestor of a Binary Search Tre.java | 35 +++++++++ ...west Common Ancestor of a Binary Tree.java | 44 +++++++++++ Yuezhu Ma/HM2/257. Binary Tree Paths.java | 32 ++++++++ ...5. Maximum Size Subarray Sum Equals K.java | 32 ++++++++ .../HM2/96. Unique Binary Search Trees.java | 74 +++++++++++++++++++ .../HM2/98. Validate Binary Search Tree.java | 28 +++++++ 10 files changed, 392 insertions(+) create mode 100644 Yuezhu Ma/HM2/101. Symmetric Tree.java create mode 100644 Yuezhu Ma/HM2/105. Construct Binary Tree from Preorder and Inord.java create mode 100644 Yuezhu Ma/HM2/106. Construct Binary Tree from Postorder and Inor.java create mode 100644 Yuezhu Ma/HM2/108. Convert Sorted Array to Binary Search Tree.java create mode 100644 Yuezhu Ma/HM2/235. Lowest Common Ancestor of a Binary Search Tre.java create mode 100644 Yuezhu Ma/HM2/236. Lowest Common Ancestor of a Binary Tree.java create mode 100644 Yuezhu Ma/HM2/257. Binary Tree Paths.java create mode 100644 Yuezhu Ma/HM2/325. Maximum Size Subarray Sum Equals K.java create mode 100644 Yuezhu Ma/HM2/96. Unique Binary Search Trees.java create mode 100644 Yuezhu Ma/HM2/98. Validate Binary Search Tree.java diff --git a/Yuezhu Ma/HM2/101. Symmetric Tree.java b/Yuezhu Ma/HM2/101. Symmetric Tree.java new file mode 100644 index 0000000..f3f7cdf --- /dev/null +++ b/Yuezhu Ma/HM2/101. Symmetric Tree.java @@ -0,0 +1,29 @@ +101. Symmetric Tree + +TEST CASE: {} + {1, 2, 2} + {1, 2, 2, 3, #, #, 3} + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public boolean isSymmetric(TreeNode root) { + if (root == null) return true; + return isSymmetricSub(root, root); + } + private boolean isSymmetricSub(TreeNode t1, TreeNode t2) { + if (t1 == null && t2 == null) return true; + if (t1 == null || t2 == null) return false; + if (t1.val != t2.val) return false; + return isSymmetricSub(t1.left, t2.right) && + isSymmetricSub(t1.right, t2.left); + } +} + diff --git a/Yuezhu Ma/HM2/105. Construct Binary Tree from Preorder and Inord.java b/Yuezhu Ma/HM2/105. Construct Binary Tree from Preorder and Inord.java new file mode 100644 index 0000000..c78db0a --- /dev/null +++ b/Yuezhu Ma/HM2/105. Construct Binary Tree from Preorder and Inord.java @@ -0,0 +1,44 @@ +105. Construct Binary Tree from Preorder and Inorder Traversal + +TEST CASE: {} + 1 + / \ + 2 3 + / \ \ + 4 5 6 + pre: (1) 2 4 5 3 6 ---> left: in中找到根1后, 左边长度为左子树:(2) 4 5 right: 同理 (3)6 + in : 4 2 5 (1) 3 6 ---> left: 根左边的: 4 (2) 5 right: 根右边的 (3)6 根的确定看pre的第一位 + + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public TreeNode buildTree(int[] preorder, int[] inorder) { + return buildTreeSub(preorder, 0, preorder.length - 1, inorder, 0, inorder.length - 1); + } + public TreeNode buildTreeSub(int[] preorder, int preStart, int preEnd, int[] inorder, int inStart, int inEnd) { + if (preStart > preEnd || inStart > inEnd) return null; + + int rootVal = preorder[preStart]; + int rootIndex = 0; + for (int i = inStart; i <= inEnd; i++) { // find root index in inorder + if (inorder[i] == rootVal) { + rootIndex = i; + break; + } + } + + int len = rootIndex - inStart; // caculate the length of left subtree + TreeNode root = new TreeNode(rootVal); // generate root node + root.left = buildTreeSub(preorder, preStart + 1, preStart + len, inorder, inStart, rootIndex - 1); // recrusive left subtree + root.right = buildTreeSub(preorder, preStart + len + 1, preEnd, inorder, rootIndex + 1, inEnd); // recrusive right subtree + return root; + } +} \ No newline at end of file diff --git a/Yuezhu Ma/HM2/106. Construct Binary Tree from Postorder and Inor.java b/Yuezhu Ma/HM2/106. Construct Binary Tree from Postorder and Inor.java new file mode 100644 index 0000000..137b7eb --- /dev/null +++ b/Yuezhu Ma/HM2/106. Construct Binary Tree from Postorder and Inor.java @@ -0,0 +1,37 @@ +106. Construct Binary Tree from Postorder and Inorder Traversal + +TEST CASE: {} + 1 + / \ + 2 3 + / \ \ + 4 5 6 + post: 4 5 2 6 3 (1) ---> left: in中找到根1后左边长度为左子树:4 5(2) right: 同理 6(3) + in : 4 2 5 (1) 3 6 ---> left: 根左边的: 4 (2) 5 right: 根右边的 (3)6 根的确定看post最后一位 + + +public TreeNode buildTree(int[] inorder, int[] postorder) { + return buildTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1); +} + + public TreeNode buildTree(int[] in, int inStart, int inEnd, int[] post, int postStart, int postEnd){ + if(inStart > inEnd || postStart > postEnd){ + return null; + } + + int rootVal = post[postEnd]; + int rootIndex = 0; + for(int i = inStart; i <= inEnd; i++){ // find root index + if(in[i] == rootVal){ + rootIndex = i; + break; + } + } + + int len = rootIndex - inStart; + TreeNode root = new TreeNode(rootVal); //generate new root + root.left = buildTree(in, inStart, rootIndex-1, post, postStart, postStart+len-1); // recurisive left subtree + root.right = buildTree(in, rootIndex+1, inEnd, post, postStart+len, postEnd-1); // recurisive right subtree + + return root; + } \ No newline at end of file diff --git a/Yuezhu Ma/HM2/108. Convert Sorted Array to Binary Search Tree.java b/Yuezhu Ma/HM2/108. Convert Sorted Array to Binary Search Tree.java new file mode 100644 index 0000000..766f0c4 --- /dev/null +++ b/Yuezhu Ma/HM2/108. Convert Sorted Array to Binary Search Tree.java @@ -0,0 +1,37 @@ +108. Convert Sorted Array to Binary Search Tree + +TEST CASE: [] + [1] + [1, 2] + [1, 2, 3] + [1, 2, 3, 4, 5, 6, 7] + + + + /** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public TreeNode sortedArrayToBST(int[] nums) { + if (nums == null) return null; + return sortedArrayToBSTSub(nums, 0, nums.length - 1); + + + } + private TreeNode sortedArrayToBSTSub(int[] nums, int start, int end) { + if (start > end) return null; + int mid = start + (end - start) / 2; + TreeNode tn = new TreeNode(nums[mid]); + tn.left = sortedArrayToBSTSub(nums, start, mid - 1); + tn.right = sortedArrayToBSTSub(nums, mid + 1, end); + return tn; + } +} + + diff --git a/Yuezhu Ma/HM2/235. Lowest Common Ancestor of a Binary Search Tre.java b/Yuezhu Ma/HM2/235. Lowest Common Ancestor of a Binary Search Tre.java new file mode 100644 index 0000000..1090731 --- /dev/null +++ b/Yuezhu Ma/HM2/235. Lowest Common Ancestor of a Binary Search Tre.java @@ -0,0 +1,35 @@ +235. Lowest Common Ancestor of a Binary Search Tree + +TEST CASE: {} p = null, q = null + 4 + / \ + 2 6 + / \ / \ + 1 3 5 7 + p = 1, q = 7 + p = 1, q = 3 + p = 7, q = 6 + + + +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (p.val <= root.val && q.val >= root.val) return root; + if (p.val < root.val && q.val < root.val) { + return lowestCommonAncestor(root.left, p, q); + } + if (p.val > root.val && q.val > root.val) { + return lowestCommonAncestor(root.right, p, q); + } + return root; + } +} \ No newline at end of file diff --git a/Yuezhu Ma/HM2/236. Lowest Common Ancestor of a Binary Tree.java b/Yuezhu Ma/HM2/236. Lowest Common Ancestor of a Binary Tree.java new file mode 100644 index 0000000..809bb28 --- /dev/null +++ b/Yuezhu Ma/HM2/236. Lowest Common Ancestor of a Binary Tree.java @@ -0,0 +1,44 @@ +236. Lowest Common Ancestor of a Binary Tree + +TEST CASE: {} + {1} p = 1; q = 1 + {1, 2, 3, 4, 5, 6, 7} p = 2 q = 5 + {1, 2, 3, 4, 5, 6, 7} p = 2 q = 3 + {1, 2, 3, 4, 5, 6, 7} p = 3 q = 8 + + + +/* + SOLUTION 1: RECURISIVE + 二治法 +*/ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { + if (root == null) return null; + if (root == p || root == q) return root; + + TreeNode left = lowestCommonAncestor(root.left, p, q); + TreeNode right = lowestCommonAncestor(root.right, p, q); + + if (left != null && right != null) return root; + if (left != null) return left; + if (right != null) return right; + return null; + } +} + + + +/* + SOLUTION 2: 假设有指针指向parent, 就可以转换成两个linkedlist merge +*/ + + diff --git a/Yuezhu Ma/HM2/257. Binary Tree Paths.java b/Yuezhu Ma/HM2/257. Binary Tree Paths.java new file mode 100644 index 0000000..4f6963c --- /dev/null +++ b/Yuezhu Ma/HM2/257. Binary Tree Paths.java @@ -0,0 +1,32 @@ +257. Binary Tree Paths + +TAST CASE: {} + {1} + {1, 2, #, 3, 4, #, #, 5} + + +// using DFS +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ +public class Solution { + public List binaryTreePaths(TreeNode root) { + ArrayList ret = new ArrayList(); + pathDFS(root, "", ret); + return ret; + } + private void pathDFS (TreeNode root, String solution, ArrayList ret) { + if (root == null) return; + if (root.left == null && root.right == null) { + ret.add (solution + root.val); + } + pathDFS(root.left, solution + root.val + "->", ret); + pathDFS(root.right, solution + root.val + "->", ret); + } +} diff --git a/Yuezhu Ma/HM2/325. Maximum Size Subarray Sum Equals K.java b/Yuezhu Ma/HM2/325. Maximum Size Subarray Sum Equals K.java new file mode 100644 index 0000000..f432c6c --- /dev/null +++ b/Yuezhu Ma/HM2/325. Maximum Size Subarray Sum Equals K.java @@ -0,0 +1,32 @@ +325. Maximum Size Subarray Sum Equals K + +TEST CASE: [], k = 0 + [1, -1, 5, -2, 3], k = 0 + [1, -1, 5, -2, 3], k = 3 + [-2, -1, 2, 1], k = 2 + +// 用hashmap存第0位到第i位的和,key为和,value为index。如果key里有 sum - k 更新max +public class Solution { + public int maxSubArrayLen(int[] nums, int k) { + if (nums == null) return 0; + + Map map = new HashMap(); + int sum = 0, max = 0; + + for(int i = 0; i < nums.length; i++) { + sum += nums[i]; + if (sum == k) { + max = i + 1; + } else if (map.containsKey(sum - k)) { + max = Math.max(max, i - map.get(sum - k)); + } + if (!map.containsKey(sum)) { + map.put (sum, i); + } + } + return max; + } +} + + + \ No newline at end of file diff --git a/Yuezhu Ma/HM2/96. Unique Binary Search Trees.java b/Yuezhu Ma/HM2/96. Unique Binary Search Trees.java new file mode 100644 index 0000000..0e709b4 --- /dev/null +++ b/Yuezhu Ma/HM2/96. Unique Binary Search Trees.java @@ -0,0 +1,74 @@ +96. Unique Binary Search Trees + +TEST CASE: n = 0; + n = 1; + n = 2; + n = 3; + n = 4; + +/* 解题思路: + 1 3 3 2 1 + \ / / / \ \ + 3 2 1 1 3 2 + / / \ \ + 2 1 2 3 + + + 比如,以1为根的树有几个,完全取决于有二个元素(right subtree 2, 3)的子树有几种。同理,2为根的子 + + 树取决于一个元素(left subtree 1 and right subtree 2)的子树有几个。以3为根的情况,则与1相同 + + (left subtree 1, 2)。* 以i为根节点的树,其左子树由[1, i-1]构成, 其右子树由[i+1, n]构成)* + + 定义Count[i] 为以[0,i]能产生的Unique Binary Tree的数目。 + + 如果数组为空,毫无疑问,只有一种BST,即空树,Count[0] =1 + + 如果数组仅有一个元素{1},只有一种BST,单个节点 Count[1] = 1 + + 如果数组有两个元素{1,2}, 那么有如下两种可能 1 2 + \ / + 2 1 + + Count[2] = Count[0] * Count[1] (1为根的情况, left null, right 一个点2) + + Count[1] * Count[0] (2为根的情况, right 一个点1, right null) + + 再看一遍三个元素的数组,可以发现BST的取值方式如下: + + Count[3] = Count[0]*Count[2] (1为根的情况, left null, right 两个点 2,3) + + Count[1]*Count[1] (2为根的情况, left 一个点 1 right 一个点 2) + + Count[2]*Count[0] (3为根的情况, left 两个点 1,2 righ null) + + 得出Count的递推公式为Count[i] = ∑ Count[0...k] * [ k+1....i] 0<=k= max) return false; + return isValidBSTSub(root.left, min, root.val) + && isValidBSTSub(root.right, root.val, max); + } +} \ No newline at end of file