Skip to content

Commit 8d428c3

Browse files
Kurt-Yuhaoel
authored andcommitted
added python solution to some problems
1 parent 6fb6205 commit 8d428c3

File tree

7 files changed

+137
-3
lines changed

7 files changed

+137
-3
lines changed

README.md

+6-3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ LeetCode
2020
|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|
2121
|655|[Print Binary Tree](https://leetcode.com/problems/print-binary-tree/) | [Python](./algorithms/python/PrintBinaryTree/printTree.py)|Medium|
2222
|643|[Maximum Average Subarray I](https://leetcode.com/problems/maximum-average-subarray-i/description/) | [C++](./algorithms/cpp/maximumAverageSubarray/MaximumAverageSubarray.I.cpp)|Easy|
23+
|623|[Add One Row to Tree](https://leetcode.com/problems/add-one-row-to-tree/) | [Python](./algorithms/python/AddOneRowToTree/addOneRow.py)|Medium|
24+
|563|[Binary Tree Tilt](https://leetcode.com/problems/binary-tree-tilt/) | [Python](./algorithms/python/BinaryTreeTilt/findTilt.py)|Easy|
25+
|543|[Diameter of Binary Tree](https://leetcode.com/problems/diameter-of-binary-tree/) | [Python](./algorithms/python/DiameterOfBinaryTree/diameterOfBinaryTree.py)|Easy|
2326
|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/) | [Python](./algorithms/python/ConvertBSTtoGreaterTree/convertBST.py)|Easy|
2427
|477|[Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/) | [C++](./algorithms/cpp/totalHammingDistance/totalHammingDistance.cpp)|Medium|
2528
|418|[SentenceScreenFitting](https://leetcode.com/problems/sentence-screen-fitting/) ♥ | [C++](./algorithms/cpp/sentenceScreenFitting/sentenceScreenFitting.cpp)|Easy|
@@ -70,7 +73,7 @@ LeetCode
7073
|342|[Power of Four](https://leetcode.com/problems/power-of-four/) | [C++](./algorithms/cpp/powerOfFour/PowerOfFour.cpp)|Easy|
7174
|341|[Flatten Nested List Iterator](https://leetcode.com/problems/flatten-nested-list-iterator/) | [C++](./algorithms/cpp/flattenNestedListIterator/FlattenNestedListIterator.cpp)|Medium|
7275
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/) | [C++](./algorithms/cpp/countingBits/CountingBits.cpp)|Medium|
73-
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium|
76+
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/) | [C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp), [Python](./algorithms/python/HouseRobberIII/rob.py)|Medium|
7477
|336|[Palindrome Pairs](https://leetcode.com/problems/palindrome-pairs/) | [C++](./algorithms/cpp/palindromePairs/PalindromePairs.cpp)|Hard|
7578
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/) | [C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
7679
|332|[Reconstruct Itinerary](https://leetcode.com/problems/reconstruct-itinerary/) | [C++](./algorithms/cpp/reconstructItinerary/ReconstructItinerary.cpp)|Medium|
@@ -170,7 +173,7 @@ LeetCode
170173
|186|[Reverse Words in a String II](https://oj.leetcode.com/problems/reverse-words-in-a-string-ii/) ♥ | [C++](./algorithms/cpp/reverseWordsInAString/reverseWordsInAString.II.cpp)|Medium|
171174
|179|[Largest Number](https://oj.leetcode.com/problems/largest-number/) | [C++](./algorithms/cpp/largestNumber/largestNumber.cpp)|Medium|
172175
|174|[Dungeon Game](https://oj.leetcode.com/problems/dungeon-game/) | [C++](./algorithms/cpp/dungeonGame/dungeonGame.cpp)|Hard|
173-
|173|[Binary Search Tree Iterator](https://oj.leetcode.com/problems/binary-search-tree-iterator/) | [C++](./algorithms/cpp/binarySearchTreeIterator/binarySearchTreeIterator.cpp), [Java](./algorithms/java/src/binarySearchTreeIterator/binarySearchTreeIterator.java)|Medium|
176+
|173|[Binary Search Tree Iterator](https://oj.leetcode.com/problems/binary-search-tree-iterator/) | [C++](./algorithms/cpp/binarySearchTreeIterator/binarySearchTreeIterator.cpp), [Java](./algorithms/java/src/binarySearchTreeIterator/binarySearchTreeIterator.java), [Python](./algorithms/python/BinarySearchTreeIterator/BSTIterator.py)|Medium|
174177
|172|[Factorial Trailing Zeroes](https://oj.leetcode.com/problems/factorial-trailing-zeroes/) | [C++](./algorithms/cpp/factorialTrailingZeroes/factorialTrailingZeroes.cpp)|Easy|
175178
|171|[Excel Sheet Column Number](https://oj.leetcode.com/problems/excel-sheet-column-number/) | [C++](./algorithms/cpp/excelSheetColumnNumber/excelSheetColumnNumber.cpp)|Easy|
176179
|170|[Two Sum III - Data structure design](https://oj.leetcode.com/problems/two-sum-iii-data-structure-design/) ♥ | [C++](./algorithms/cpp/twoSum/twoSum.III.cpp)|Easy|
@@ -198,7 +201,7 @@ LeetCode
198201
|148|[Sort List](https://oj.leetcode.com/problems/sort-list/)| [C++](./algorithms/cpp/sortList/sortList.cpp)|Medium|
199202
|147|[Insertion Sort List](https://oj.leetcode.com/problems/insertion-sort-list/)| [C++](./algorithms/cpp/insertionSortList/insertionSortList.cpp)|Medium|
200203
|146|[LRU Cache](https://oj.leetcode.com/problems/lru-cache/)| [C++](./algorithms/cpp/LRUCache/LRUCache.cpp), [Java](./algorithms/java/src/lruCache/LRUCache.java)|Hard|
201-
|145|[Binary Tree Postorder Traversal](https://oj.leetcode.com/problems/binary-tree-postorder-traversal/)| [C++](./algorithms/cpp/binaryTreePostorderTraversal/binaryTreePostorderTraversal.cpp)|Hard|
204+
|145|[Binary Tree Postorder Traversal](https://oj.leetcode.com/problems/binary-tree-postorder-traversal/)| [C++](./algorithms/cpp/binaryTreePostorderTraversal/binaryTreePostorderTraversal.cpp), [Python](./algorithms/python/BinaryTreePostorderTraversal/postorderTraversal.py)|Hard|
202205
|144|[Binary Tree Preorder Traversal](https://oj.leetcode.com/problems/binary-tree-preorder-traversal/)| [C++](./algorithms/cpp/binaryTreePreorderTraversal/binaryTreePreorderTraversal.cpp), [Java](./algorithms/java/src/binaryTreePreorderTraversal/binaryTreePreorderTraversal.java)|Medium|
203206
|143|[Reorder List](https://oj.leetcode.com/problems/reorder-list/)| [C++](./algorithms/cpp/reorderList/reorderList.cpp)|Medium|
204207
|142|[Linked List Cycle II](https://oj.leetcode.com/problems/linked-list-cycle-ii/)| [C++](./algorithms/cpp/linkedListCycle/linkedListCycle.II.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# get the nodes at (d - 1) layer:
2+
3+
def addOneRow(self, root, v, d):
4+
dummy, dummy.left = TreeNode(v), root
5+
row = [dummy]
6+
for _ in range(d - 1):
7+
row = [kid for node in row for kid in (node.left, node.right) if kid]
8+
9+
for node in row:
10+
node.left, node.left.left = TreeNode(v), node.left
11+
node.right, node.right.right = TreeNode(v), node.right
12+
return dummy.left
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# straight forward solution: do a inorder traversal
2+
3+
class BSTIterator(object):
4+
def __init__(self, root):
5+
self.inorder = []
6+
def helper(root):
7+
if root:
8+
helper(root.left)
9+
self.inorder.append(root.val)
10+
helper(root.right)
11+
helper(root)
12+
13+
def hasNext(self):
14+
return len(self.inorder) != 0
15+
16+
def next(self):
17+
return self.inorder.pop(0)
18+
19+
# Alternative solution:
20+
class BSTIterator(object):
21+
def __init__(self, root):
22+
self.stack = []
23+
while root:
24+
self.stack.append(root)
25+
root = root.left
26+
27+
28+
def hasNext(self):
29+
return len(self.stack) != 0
30+
31+
32+
def next(self):
33+
node = self.stack.pop()
34+
x = node.right
35+
while x:
36+
self.stack.append(x)
37+
x = x.left
38+
return node.val
39+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Method 1: recursive
3+
"""
4+
def postorderTraversal(self, root):
5+
res = []
6+
def helper(root):
7+
if root:
8+
helper(root.left)
9+
helper(root.right)
10+
res.append(root.val)
11+
helper(root)
12+
return res
13+
14+
"""
15+
Method 2: iterative, using two stacks
16+
initialize first stack with root node, do the following if first is not None:
17+
pop from first stack, append the popped element to second stack,
18+
add left and right node to first stack if they are not None
19+
return the second stack's value in reverse order
20+
"""
21+
def postorderTraversal(self, root):
22+
if not root: return []
23+
first, second = [root], []
24+
while first:
25+
node = first.pop()
26+
second.append(node)
27+
if node.left: first.append(node.left)
28+
if node.right: first.append(node.right)
29+
res = [node.val for node in second]
30+
res.reverse()
31+
return res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
def findTilt(self, root):
2+
self.res = 0
3+
def helper(root):
4+
if not root: return 0
5+
left = helper(root.left)
6+
right = helper(root.right)
7+
self.res += abs(left - right)
8+
return root.val + left + right
9+
helper(root)
10+
return self.res
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
def diameterOfBinaryTree(self, root):
2+
self.res = 0
3+
def helper(root):
4+
if not root: return 0
5+
left, right = helper(root.left), helper(root.right)
6+
self.res = max(self.res, left + right)
7+
return 1 + max(left, right)
8+
helper(root)
9+
return self.res
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
"""
2+
Answe inspired by the post from here:
3+
https://leetcode.com/problems/house-robber-iii/discuss/79330/Step-by-step-tackling-of-the-problem
4+
"""
5+
# Method 1: dynamic programming solution:
6+
def rob1(self, root):
7+
lookup = {}
8+
def helper(root):
9+
if not root: return 0
10+
if root in lookup: return lookup[root]
11+
val = 0
12+
13+
if root.left:
14+
val += helper(root.left.left) + helper(root.left.right)
15+
if root.right:
16+
val += helper(root.right.left) + helper(root.right.right)
17+
val = max(val + root.val, helper(root.left) + helper(root.right))
18+
lookup[root] = val
19+
return val
20+
return helper(root)
21+
22+
# Method 2: Greedy approach:
23+
def rob2(self, root):
24+
def helper(root):
25+
if not root: return [0, 0]
26+
left, right = helper(root.left), helper(root.right)
27+
not_robbed = max(left) + max(right)
28+
robbed = root.val + left[0] + right[0]
29+
return [not_robbed, robbed]
30+
return max(helper(root))

0 commit comments

Comments
 (0)