Skip to content

Commit d7da0ac

Browse files
solves binary tree post order traversal
1 parent 4b2e6ef commit d7da0ac

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | [![Java](assets/java.png)](src/ValidPalindrome.java) [![Python](assets/python.png)](python/valid_palindrome.py) |
4848
| 136 | [Single Number](https://leetcode.com/problems/single-number) | [![Java](assets/java.png)](src/SingleNumber.java) [![Python](assets/python.png)](python/single_number.py) |
4949
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | [![Java](assets/java.png)](src/LinkedListCycle.java) [![Python](assets/python.png)](python/linked_list_cycle.py) |
50-
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [![Java](assets/java.png)](src/BinaryTreePreOrderTraversal.java) [![Python](assets/python.png)](python/binary_tree_preorder_traversal.py) |
50+
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal) | [![Java](assets/java.png)](src/BinaryTreePreOrderTraversal.java) [![Python](assets/python.png)](python/binary_tree_preorder_traversal.py) |
51+
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal) | [![Java](assets/java.png)](src/BinaryTreePostorderTraversal.java) [![Python](assets/python.png)](python/binary_tree_postorder_traversal.py) |
5152
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | [![Java](assets/java.png)](src/MinStack.java) [![Python](assets/python.png)](python/min_stack.py) |
5253
| 157 | [Read N Characters Given Read4](https://leetcode.com/problems/read-n-characters-given-read4) | |
5354
| 160 | [Intersection of Two Linked Lists](https://leetcode.com/problems/intersection-of-two-linked-lists) | [![Java](assets/java.png)](src/IntersectionOf2LinkedLists.java) [![Python](assets/python.png)](python/intersecction_of_two_linked_lists.py) |
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Definition for a binary tree node.
2+
from typing import Optional, List
3+
4+
5+
class TreeNode:
6+
def __init__(self, val=0, left=None, right=None):
7+
self.val = val
8+
self.left = left
9+
self.right = right
10+
11+
12+
class Solution:
13+
def _postorder_traversal(self, root: Optional[TreeNode], list: List[int]) -> None:
14+
if root is None: return
15+
self._postorder_traversal(root.left, list)
16+
self._postorder_traversal(root.right, list)
17+
list.append(root.val)
18+
19+
def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
20+
result = []
21+
self._postorder_traversal(root, result)
22+
return result

src/BinaryTreePostorderTraversal.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
public class BinaryTreePostorderTraversal {
5+
public List<Integer> postorderTraversal(TreeNode root) {
6+
List<Integer> result = new ArrayList<>();
7+
postorderTraversal(root, result);
8+
return result;
9+
}
10+
11+
private void postorderTraversal(TreeNode root, List<Integer> list) {
12+
if (root == null) return;
13+
postorderTraversal(root.left, list);
14+
postorderTraversal(root.right, list);
15+
list.add(root.val);
16+
}
17+
}

0 commit comments

Comments
 (0)