forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added python solutions to some problems
- Loading branch information
Showing
9 changed files
with
161 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
algorithms/python/BinaryTreeZigzagLevelOrderTraversal/zigzagLevelOrder.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
11 changes: 11 additions & 0 deletions
11
algorithms/python/FlattenBinaryTreeToLinkedList/flatten.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
13 changes: 13 additions & 0 deletions
13
algorithms/python/MaximumWidthOfBinaryTree/widthOfBinaryTree.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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] |