diff --git a/README.md b/README.md index 347798bc4..e93cefb40 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,17 @@ LeetCode |771|[Jewels and Stones](https://leetcode.com/problems/jewels-and-stones/description) | [C++](./algorithms/cpp/jewelsAndStones/JewelsAndStones.cpp)|Easy| |684|[Redundant Connection](https://leetcode.com/problems/redundant-connection/) | [Python](./algorithms/python/RedundantConnection/findRedundantConnection.py)|Medium| |671|[Second Minimum Node In a Binary Tree](https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/) | [Python](./algorithms/python/SecondMinimumNodeInABinaryTree/findSecondMinimumValue.py)|Easy| +|662|[Maximum Width of Binary Tree](https://leetcode.com/problems/maximum-width-of-binary-tree/) | [Python](./algorithms/python/MaximumWidthOfBinaryTree/widthOfBinaryTree.py)|Medium| |655|[Print Binary Tree](https://leetcode.com/problems/print-binary-tree/) | [Python](./algorithms/python/PrintBinaryTree/printTree.py)|Medium| |643|[Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/description/) | [C++](./algorithms/cpp/maximumAverageSubarray/MaximumAverageSubarray.I.cpp)|Easy| |623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | [Python](./algorithms/python/AddOneRowToTree/addOneRow.py)|Medium| +|572|[Subtree of Another Tree](https://leetcode.com/problems/subtree-of-another-tree/) | [Python](./algorithms/python/SubtreeOfAnotherTree/isSubtree.py)|Easy| |563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [Python](./algorithms/python/BinaryTreeTilt/findTilt.py)|Easy| |543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | [Python](./algorithms/python/DiameterOfBinaryTree/diameterOfBinaryTree.py)|Easy| |538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/) | [Python](./algorithms/python/ConvertBSTtoGreaterTree/convertBST.py)|Easy| |477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [C++](./algorithms/cpp/totalHammingDistance/totalHammingDistance.cpp)|Medium| +|450|[DeleteNodeInABST](https://leetcode.com/problems/delete-node-in-a-bst/) | [Python](./algorithms/python/DeleteNodeInABST/deleteNode.py)|Medium| +|449|[Serialize and Deserialize BST](https://leetcode.com/problems/serialize-and-deserialize-bst/) | [Python](./algorithms/python/SerializeAndDeserializeBST/serialize.py)|Medium| |418|[SentenceScreenFitting](https://leetcode.com/problems/sentence-screen-fitting/) ♥ | [C++](./algorithms/cpp/sentenceScreenFitting/sentenceScreenFitting.cpp)|Easy| |416|[Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/description/) | [C++](./algorithms/cpp/partitionEqualSubsetSum/PartitionEqualSubsetSum.cpp)|Medium| |415|[Add Strings](https://leetcode.com/problems/add-strings/) | [C++](./algorithms/cpp/addStrings/AddStrings.cpp)|Easy| @@ -217,7 +221,7 @@ LeetCode |132|[Palindrome Partitioning II](https://oj.leetcode.com/problems/palindrome-partitioning-ii/)| [C++](./algorithms/cpp/palindromePartitioning/palindromePartitioning.II.cpp)|Hard| |131|[Palindrome Partitioning](https://oj.leetcode.com/problems/palindrome-partitioning/)| [C++](./algorithms/cpp/palindromePartitioning/palindromePartitioning.cpp)|Medium| |130|[Surrounded Regions](https://oj.leetcode.com/problems/surrounded-regions/)| [C++](./algorithms/cpp/surroundedRegions/surroundedRegions.cpp)|Medium| -|129|[Sum Root to Leaf Numbers](https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/)| [C++](./algorithms/cpp/sumRootToLeafNumber/sumRootToLeafNumber.cpp)|Medium| +|129|[Sum Root to Leaf Numbers](https://oj.leetcode.com/problems/sum-root-to-leaf-numbers/)| [C++](./algorithms/cpp/sumRootToLeafNumber/sumRootToLeafNumber.cpp), [Python](./algorithms/python/SumRootToLeafNumbers/sumNumbers.py)|Medium| |128|[Longest Consecutive Sequence](https://oj.leetcode.com/problems/longest-consecutive-sequence/)| [C++](./algorithms/cpp/longestConsecutiveSequence/longestConsecutiveSequence.cpp)|Hard| |127|[Word Ladder](https://oj.leetcode.com/problems/word-ladder/)| [C++](./algorithms/cpp/wordLadder/wordLadder.cpp)|Medium| |126|[Word Ladder II](https://oj.leetcode.com/problems/word-ladder-ii/)| [C++](./algorithms/cpp/wordLadder/wordLadder.II.cpp)|Hard| @@ -232,7 +236,7 @@ LeetCode |117|[Populating Next Right Pointers in Each Node II](https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node-ii/)| [C++](./algorithms/cpp/populatingNextRightPointersInEachNode/populatingNextRightPointersInEachNode.II.cpp)|Hard| |116|[Populating Next Right Pointers in Each Node](https://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/)| [C++](./algorithms/cpp/populatingNextRightPointersInEachNode/populatingNextRightPointersInEachNode.cpp)|Medium| |115|[Distinct Subsequences](https://oj.leetcode.com/problems/distinct-subsequences/)| [C++](./algorithms/cpp/distinctSubsequences/distinctSubsequences.cpp)|Hard| -|114|[Flatten Binary Tree to Linked List](https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/)| [C++](./algorithms/cpp/flattenBinaryTreeToLinkedList/flattenBinaryTreeToLinkedList.cpp)|Medium| +|114|[Flatten Binary Tree to Linked List](https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/)| [C++](./algorithms/cpp/flattenBinaryTreeToLinkedList/flattenBinaryTreeToLinkedList.cpp), [Python](./algorithms/python/FlattenBinaryTreeToLinkedList/flatten.py)|Medium| |113|[Path Sum II](https://oj.leetcode.com/problems/path-sum-ii/)| [C++](./algorithms/cpp/pathSum/pathSum.II.cpp)|Medium| |112|[Path Sum](https://oj.leetcode.com/problems/path-sum/)| [C++](./algorithms/cpp/pathSum/pathSum.cpp)|Easy| |111|[Minimum Depth of Binary Tree](https://oj.leetcode.com/problems/minimum-depth-of-binary-tree/)| [C++](./algorithms/cpp/minimumDepthOfBinaryTree/minimumDepthOfBinaryTree.cpp)|Easy| @@ -243,14 +247,14 @@ LeetCode |106|[Construct Binary Tree from Inorder and Postorder Traversal](https://oj.leetcode.com/problems/construct-binary-tree-from-inorder-and-postorder-traversal/)| [C++](./algorithms/cpp/constructBinaryTreeFromInorderAndPostorderTraversal/constructBinaryTreeFromInorderAndPostorderTraversal.cpp)|Medium| |105|[Construct Binary Tree from Preorder and Inorder Traversal](https://oj.leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/)| [C++](./algorithms/cpp/constructBinaryTreeFromPreorderAndInorderTraversal/constructBinaryTreeFromPreorderAndInorderTraversal.cpp)|Medium| |104|[Maximum Depth of Binary Tree](https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/)| [C++](./algorithms/cpp/maximumDepthOfBinaryTree/maximumDepthOfBinaryTree.cpp), [Java](./algorithms/java/src/maximumDepthOfBinaryTree/maximumDepthOfBinaryTree.java)|Easy| -|103|[Binary Tree Zigzag Level Order Traversal](https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| [C++](./algorithms/cpp/binaryTreeZigzagLevelOrderTraversal/binaryTreeZigzagLevelOrderTraversal.cpp)|Medium| +|103|[Binary Tree Zigzag Level Order Traversal](https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)| [C++](./algorithms/cpp/binaryTreeZigzagLevelOrderTraversal/binaryTreeZigzagLevelOrderTraversal.cpp), [Python](./algorithms/python/BinaryTreeZigzagLevelOrderTraversal/zigzagLevelOrder.py)|Medium| |102|[Binary Tree Level Order Traversal](https://oj.leetcode.com/problems/binary-tree-level-order-traversal/)| [C++](./algorithms/cpp/binaryTreeLevelOrderTraversal/binaryTreeLevelOrderTraversal.cpp), [Java](./algorithms/java/src/binaryTreeLevelOrderTraversal/binaryTreeLevelOrderTraversal.java)|Easy| |101|[Symmetric Tree](https://oj.leetcode.com/problems/symmetric-tree/)| [C++](./algorithms/cpp/symmetricTree/symmetricTree.cpp)|Easy| |100|[Same Tree](https://oj.leetcode.com/problems/same-tree/)| [C++](./algorithms/cpp/sameTree/sameTree.cpp)|Easy| |99|[Recover Binary Search Tree](https://oj.leetcode.com/problems/recover-binary-search-tree/)| [C++](./algorithms/cpp/recoverBinarySearchTree/recoverBinarySearchTree.cpp)|Hard| |98|[Validate Binary Search Tree](https://oj.leetcode.com/problems/validate-binary-search-tree/)| [C++](./algorithms/cpp/validateBinarySearchTree/validateBinarySearchTree.cpp), [Java](./algorithms/java/src/validateBinarySearchTree/validateBinarySearchTree.java)|Medium| |97|[Interleaving String](https://oj.leetcode.com/problems/interleaving-string/)| [C++](./algorithms/cpp/interleavingString/interleavingString.cpp)|Hard| -|96|[Unique Binary Search Trees](https://oj.leetcode.com/problems/unique-binary-search-trees/)| [C++](./algorithms/cpp/uniqueBinarySearchTrees/uniqueBinarySearchTrees.cpp)|Medium| +|96|[Unique Binary Search Trees](https://oj.leetcode.com/problems/unique-binary-search-trees/)| [C++](./algorithms/cpp/uniqueBinarySearchTrees/uniqueBinarySearchTrees.cpp), [Python](./algorithms/python/UniqueBinarySearchTrees/numTrees.py)|Medium| |95|[Unique Binary Search Trees II](https://oj.leetcode.com/problems/unique-binary-search-trees-ii/)| [C++](./algorithms/cpp/uniqueBinarySearchTrees/uniqueBinarySearchTrees.II.cpp)|Medium| |94|[Binary Tree Inorder Traversal](https://oj.leetcode.com/problems/binary-tree-inorder-traversal/)| [C++](./algorithms/cpp/binaryTreeInorderTraversal/binaryTreeInorderTraversal.cpp)|Medium| |93|[Restore IP Addresses](https://oj.leetcode.com/problems/restore-ip-addresses/)| [C++](./algorithms/cpp/restoreIPAddresses/restoreIPAddresses.cpp)|Medium| diff --git a/algorithms/python/BinaryTreeZigzagLevelOrderTraversal/zigzagLevelOrder.py b/algorithms/python/BinaryTreeZigzagLevelOrderTraversal/zigzagLevelOrder.py new file mode 100644 index 000000000..71c5ed75f --- /dev/null +++ b/algorithms/python/BinaryTreeZigzagLevelOrderTraversal/zigzagLevelOrder.py @@ -0,0 +1,18 @@ +""" +simple bfs +""" + +def zigzagLevelOrder(self, root): + if not root: return [] + stack = [[root]] + res = [[root.val]] + level = 0 + while True: + level += 1 + children = [child for node in stack[-1] for child in (node.left, node.right) if child] + if not children: break + temp = [node.val for node in children] + if level % 2 == 1: temp.reverse() + res.append(temp) + stack.append(children) + return res \ No newline at end of file diff --git a/algorithms/python/DeleteNodeInABST/deleteNode.py b/algorithms/python/DeleteNodeInABST/deleteNode.py new file mode 100644 index 000000000..2c50e85f2 --- /dev/null +++ b/algorithms/python/DeleteNodeInABST/deleteNode.py @@ -0,0 +1,32 @@ +""" +case 1: if node we want to delete has no children: simply delete it +case 2: if it has only one child, then replace it with its child +case 3: if it has two children, first find the inorder successor (or predecessor), + then replace the node's value with successor's value, and finally delete this successor +""" + +def deleteNode(self, root, key): + if not root: return None + if key < root.val: + root.left = self.deleteNode(root.left, key) + elif key > root.val: + root.right = self.deleteNode(root.right, key) + else: + # if this node has only one child or no child: + if not root.left: + temp = root.right + root = None + return temp + elif not root.right: + temp = root.left + root = None + return temp + + # otherwise, find the inorder successor: + curr = root.right + while curr.left: + curr = curr.left + + root.val = curr.val + root.right = self.deleteNode(root.right, curr.val) + return root \ No newline at end of file diff --git a/algorithms/python/FlattenBinaryTreeToLinkedList/flatten.py b/algorithms/python/FlattenBinaryTreeToLinkedList/flatten.py new file mode 100644 index 000000000..7a7af3c97 --- /dev/null +++ b/algorithms/python/FlattenBinaryTreeToLinkedList/flatten.py @@ -0,0 +1,11 @@ +class Solution(object): + def __init__(self): + self.prev = None + + def flatten(self, root): + if not root: return None + self.flatten(root.right) + self.flatten(root.left) + root.right = self.prev + root.left = None + self.prev = root \ No newline at end of file diff --git a/algorithms/python/MaximumWidthOfBinaryTree/widthOfBinaryTree.py b/algorithms/python/MaximumWidthOfBinaryTree/widthOfBinaryTree.py new file mode 100644 index 000000000..076717e4e --- /dev/null +++ b/algorithms/python/MaximumWidthOfBinaryTree/widthOfBinaryTree.py @@ -0,0 +1,13 @@ +def widthOfBinaryTree(self, root): + if not root: return 0 + stack = [[(root, 0)]] + res = 1 + while True: + children = [] + for node, value in stack[-1]: + if node.left: children.append((node.left, value * 2)) + if node.right: children.append((node.right, value * 2 + 1)) + if not children: break + stack.append(children) + res = max(res, children[-1][1] - children[0][1] + 1) + return res \ No newline at end of file diff --git a/algorithms/python/SerializeAndDeserializeBST/serialize.py b/algorithms/python/SerializeAndDeserializeBST/serialize.py new file mode 100644 index 000000000..efe975b2c --- /dev/null +++ b/algorithms/python/SerializeAndDeserializeBST/serialize.py @@ -0,0 +1,26 @@ +class Codec: + + def serialize(self, root): + preorder = [] + + def helper(node): + if node: + preorder.append(node.val) + helper(node.left) + helper(node.right) + helper(root) + return ' '.join(map(str, preorder)) + + + def deserialize(self, data): + vals = collections.deque(int(val) for val in data.split()) + + def build(minval, maxval): + if vals and minval < vals[0] < maxval: + val = vals.popleft() + node = TreeNode(val) + node.left = build(minval, val) + node.right = build(val, maxval) + return node + + return build(float('-infinity'), float('infinity')) \ No newline at end of file diff --git a/algorithms/python/SubtreeOfAnotherTree/isSubtree.py b/algorithms/python/SubtreeOfAnotherTree/isSubtree.py new file mode 100644 index 000000000..afb242470 --- /dev/null +++ b/algorithms/python/SubtreeOfAnotherTree/isSubtree.py @@ -0,0 +1,15 @@ +def isSubtree(self, s, t): + stack = [s] + while stack: + node = stack.pop(0) + if node.val == t.val: + if self.check(node, t): return True + stack += [child for child in [node.left, node.right] if child] + return False + +def check(self, first, second): + if not first and not second: + return True + if first and second: + return first.val == second.val and self.check(first.left, second.left) and self.check(first.right, second.right) + return False \ No newline at end of file diff --git a/algorithms/python/SumRootToLeafNumbers/sumNumbers.py b/algorithms/python/SumRootToLeafNumbers/sumNumbers.py new file mode 100644 index 000000000..644bc1505 --- /dev/null +++ b/algorithms/python/SumRootToLeafNumbers/sumNumbers.py @@ -0,0 +1,29 @@ +""" +Method 1: dfs +""" +def sumNumbers(self, root): + if not root: return 0 + stack, res = [(root, root.val)], 0 + while stack: + node, value = stack.pop() + if not node.left and not node.right: + res += value + if node.right: + stack.append((node.right, value * 10 + node.right.val)) + if node.left: + stack.append((node.left, value * 10 + node.left.val)) + return res + + + +""" +Method 2: recursive solution +""" +def sumNumbers(self, root): + return self.helper(root, 0) + +def helper(self, node, s): + if not node: return 0 + if not node.left and not node.right: return s * 10 + node.val + return self.helper(node.left, s * 10 + node.val) + \ + self.helper(node.right, s * 10 + node.val) \ No newline at end of file diff --git a/algorithms/python/UniqueBinarySearchTrees/numTrees.py b/algorithms/python/UniqueBinarySearchTrees/numTrees.py new file mode 100644 index 000000000..f0ab85e90 --- /dev/null +++ b/algorithms/python/UniqueBinarySearchTrees/numTrees.py @@ -0,0 +1,9 @@ +def numTrees(self, n): + if n == 1: return 1 + res = [1, 1] + for i in range(2, n + 1): + val = 0 + for j in range(i): + val += res[j] * res[i - j - 1] + res.append(val) + return res[n] \ No newline at end of file