Skip to content

Commit bdaec07

Browse files
committed
94_Binary_Tree_Inorder_Traversal
145_Binary_Tree_Postorder_Traversal
1 parent 0931d9e commit bdaec07

File tree

3 files changed

+52
-17
lines changed

3 files changed

+52
-17
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
| 66 | [Plus One](https://leetcode.com/problems/plus-one/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/66_Plus_One.py) | Check if current digit == 9. |
3434
| 70 | [Climbing Stairs](https://leetcode.com/problems/climbing-stairs/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/70_Climbing_Stairs.py) | Bottom-up DP, dp[i] = dp[i - 2] + dp[i- 1] <br>1. O(n) and O(n)<br>2. Only two variables are needed, O(n) and O(1) |
3535
| 72 | [Edit Distance](https://leetcode.com/problems/edit-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/72_Edit_Distance.py) | [Background](https://en.wikipedia.org/wiki/Edit_distance)<br> 1. DP O(n^2) space<br>2. DP O(n) space |
36+
| 94 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/94_Binary_Tree_Inorder_Traversal.py) | 1. Recursion, O(n) and O(1)<br>2. Stack and check isinstance(curr, TreeNode), O(n) and O(n)<br>3. Stack and check left and right, O(n) and O(n) |
3637
| 98 | [Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/98_Validate_Binary_Search_Tree.py) | 1. Stack O(n) and O(n)<br>2. Recursion O(n) and O(n) |
3738
| 104 | [Maximum Depth of Binary Tree](https://leetcode.com/problems/maximum-depth-of-binary-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/104_Maximum_Depth_of_Binary_Tree.py)| Recursion max(left, right) + 1 |
3839
| 108 | [Convert Sorted Array to Binary Search Tree](https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/108_Convert_Sorted_Array_to_Binary_Search_Tree.py)| Recursion O(n) and O(nlgn)|
@@ -50,7 +51,7 @@
5051
| 142 | [Linked List Cycle II](https://discuss.leetcode.com/topic/2975/o-n-solution-by-using-two-pointers-without-change-anything) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/142_Linked_List_Cycle_II.py) | Two points, a+b=nr |
5152
| 143 | [Reorder List](https://leetcode.com/problems/reorder-list/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/143_Reorder_List.py) | 1. List as index to rebuild relation, O(n) and O(n)<br>2. Two points, O(n) and O(1) |
5253
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/144_Binary_Tree_Preorder_Traversal.py) | 1. Recursion, O(n) and O(n)<br>2. Stack, O(n) and O(n) |
53-
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/145_Binary_Tree_Postorder_Traversal.py) | 1. Recursion, O(n) and O(n)<br>2. Stack, O(n) and O(n) |
54+
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/145_Binary_Tree_Postorder_Traversal.py) | 1. Recursion, O(n) and O(n)<br>2. Stack and queue (insert 0), O(n) and O(n)<br>3. Stack and isinstance(curr, TreeNode), O(n) and O(n) |
5455
| 146 | [LRU Cache](https://leetcode.com/problems/lru-cache/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/146_LRU_Cache.py) | 1. Queue and dict<br>2. OrderedDict |
5556
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/150_Evaluate_Reverse_Polish_Notation.py) | Stack |
5657
| 151 | [Reverse Words in a String](https://discuss.leetcode.com/category/159/reverse-words-in-a-string) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/151_Reverse_Words_in_a_String.py)| 1. Python split by space <br>2. Reverse all and reverse words |

python/145_Binary_Tree_Postorder_Traversal.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,33 @@ class Solution(object):
2525
# self.postorderHelp(node.right, stack)
2626
# stack.append(node.val)
2727

28+
# def postorderTraversal(self, root):
29+
# # Stack
30+
# if root is None:
31+
# return []
32+
# res = []
33+
# stack = [root]
34+
# while len(stack) > 0:
35+
# curr = stack.pop()
36+
# res.insert(0, curr.val)
37+
# if curr.left is not None:
38+
# stack.append(curr.left)
39+
# if curr.right is not None:
40+
# stack.append(curr.right)
41+
# return res
42+
2843
def postorderTraversal(self, root):
29-
# Stack
3044
if root is None:
3145
return []
32-
res = []
33-
stack = [root]
46+
res = []; stack = [root]
3447
while len(stack) > 0:
3548
curr = stack.pop()
36-
res.insert(0, curr.val)
37-
if curr.left is not None:
38-
stack.append(curr.left)
49+
if not isinstance(curr, TreeNode):
50+
res.append(curr)
51+
continue
52+
stack.append(curr.val)
3953
if curr.right is not None:
4054
stack.append(curr.right)
55+
if curr.left is not None:
56+
stack.append(curr.left)
4157
return res

python/94_Binary_Tree_Inorder_Traversal.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,36 @@ class Solution(object):
4444
# queue.insert(0, curr.left)
4545
# curr.left = None
4646
# return res
47+
# def inorderTraversal(self, root):
48+
# res = []
49+
# stack = []
50+
# while root is not None:
51+
# stack.append(root)
52+
# root = root.left
53+
# while root is None:
54+
# if len(stack) == 0:
55+
# return res
56+
# root = stack.pop()
57+
# res.append(root.val)
58+
# root = root.right
59+
# return res
60+
4761
def inorderTraversal(self, root):
62+
if root is None:
63+
return []
4864
res = []
49-
stack = []
50-
while root is not None:
51-
stack.append(root)
52-
root = root.left
53-
while root is None:
54-
if len(stack) == 0:
55-
return res
56-
root = stack.pop()
57-
res.append(root.val)
58-
root = root.right
65+
stack = [root]
66+
while len(stack) > 0:
67+
curr = stack.pop()
68+
if not isinstance(curr, TreeNode):
69+
res.append(curr)
70+
continue
71+
if curr.right is not None:
72+
stack.append(curr.right)
73+
stack.append(curr.val)
74+
if curr.left is not None:
75+
stack.append(curr.left)
5976
return res
6077

6178

79+

0 commit comments

Comments
 (0)