Skip to content

Commit a4654f1

Browse files
committed
leetcode
1 parent 90e7585 commit a4654f1

File tree

4 files changed

+247
-0
lines changed

4 files changed

+247
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
3+
-* Binary Tree Postorder Traversal *-
4+
Given the root of a binary tree, return the postorder traversal of its nodes' values.
5+
6+
7+
8+
Example 1:
9+
10+
11+
Input: root = [1,null,2,3]
12+
Output: [3,2,1]
13+
Example 2:
14+
15+
Input: root = []
16+
Output: []
17+
Example 3:
18+
19+
Input: root = [1]
20+
Output: [1]
21+
22+
23+
Constraints:
24+
25+
The number of the nodes in the tree is in the range [0, 100].
26+
-100 <= Node.val <= 100
27+
28+
29+
Follow up: Recursive solution is trivial, could you do it iteratively?
30+
31+
*/
32+
33+
// Definition for a binary tree node.
34+
import 'dart:collection';
35+
36+
class TreeNode {
37+
int val;
38+
TreeNode? left;
39+
TreeNode? right;
40+
TreeNode([this.val = 0, this.left, this.right]);
41+
}
42+
43+
class C {
44+
/*
45+
3. Postorder Traversal
46+
Step 1: Traverse the left subtree, i.e., call Preorder(left->subtree)
47+
Step 2: Traverse the right subtree, i.e., call Inorder(right->subtree)
48+
Step 3: Visit the root.
49+
50+
*/
51+
// Runtime: 513 ms, faster than 18.18% of Dart online submissions for Binary Tree Postorder Traversal.
52+
// Memory Usage: 140.7 MB, less than 36.36% of Dart online submissions for Binary Tree Postorder Traversal.
53+
List<int> postorderTraversal(TreeNode? root) {
54+
List<int> ans = List.empty(growable: true);
55+
solve(root, ans);
56+
return ans;
57+
}
58+
59+
void solve(TreeNode? root, List<int> ans) {
60+
// Base case
61+
if (root == null) return;
62+
63+
// Preorder Traversal = LEFT - RIGHT - ROOT
64+
solve(root.left, ans);
65+
solve(root.right, ans);
66+
ans.add(root.val);
67+
}
68+
}
69+
70+
class E {
71+
// (Iterative Approach Using Stack
72+
// Runtime: 462 ms, faster than 63.64% of Dart online submissions for Binary Tree Postorder Traversal.
73+
// Memory Usage: 143.1 MB, less than 18.18% of Dart online submissions for Binary Tree Postorder Traversal.
74+
List<int> postorderTraversal(TreeNode? root) {
75+
// Create an array list to store the solution result...
76+
List<int> sol = List.empty(growable: true);
77+
// Return the solution answer if the tree is empty...
78+
if (root == null) return sol;
79+
// Create an empty stack and push the root node...
80+
Queue<TreeNode?> bag = Queue();
81+
bag.add(root);
82+
// Loop till stack is empty...
83+
while (!bag.isEmpty) {
84+
// set peek a node from the stack...
85+
TreeNode? node = bag.last;
86+
// If the subtrees of that node are null, then pop & store the pop value into solution result...
87+
if (node?.left == null && node?.right == null) {
88+
TreeNode? pop = bag.removeLast();
89+
sol.add(pop!.val);
90+
} else {
91+
// Push the right child of the popped node into the stack...
92+
if (node?.right != null) {
93+
bag.add(node?.right);
94+
node?.right = null;
95+
}
96+
// Push the left child of the popped node into the stack...
97+
if (node?.left != null) {
98+
bag.add(node?.left);
99+
node?.left = null;
100+
}
101+
}
102+
}
103+
return sol; // Return the solution list...
104+
}
105+
}
106+
107+
class F {
108+
// Runtime: 465 ms, faster than 63.64% of Dart online submissions for Binary Tree Postorder Traversal.
109+
// Memory Usage: 140.4 MB, less than 63.64% of Dart online submissions for Binary Tree Postorder Traversal.
110+
List<int> list = [];
111+
List<int> postorderTraversal(TreeNode? root) {
112+
if (root == null) return list;
113+
114+
postorderTraversal(root.left);
115+
postorderTraversal(root.right);
116+
list.add(root.val);
117+
return list;
118+
}
119+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package main
2+
3+
func postorderTraversal(root *TreeNode) []int {
4+
5+
// empty tree
6+
if root == nil {
7+
return []int{}
8+
}
9+
10+
nodeStack := []*TreeNode{root} // the nodes
11+
nodesSeen := []bool{false} // whether a node already seen
12+
values := make([]int, 0) // values in postorder
13+
14+
// traverse tree with stack
15+
for len(nodeStack) != 0 {
16+
node := nodeStack[len(nodeStack)-1]
17+
18+
// leaf -> node seen; or children traversed
19+
if (node.Left == nil && node.Right == nil) || nodesSeen[len(nodesSeen)-1] {
20+
values = append(values, node.Val)
21+
nodeStack = nodeStack[:len(nodeStack)-1] // pop node
22+
nodesSeen = nodesSeen[:len(nodesSeen)-1]
23+
continue
24+
}
25+
26+
nodesSeen[len(nodesSeen)-1] = true // mark node as seen
27+
28+
// push children on stack
29+
if node.Right != nil {
30+
nodeStack = append(nodeStack, node.Right)
31+
nodesSeen = append(nodesSeen, false)
32+
}
33+
if node.Left != nil { // traversed first, therefore put on stack second
34+
nodeStack = append(nodeStack, node.Left)
35+
nodesSeen = append(nodesSeen, false)
36+
}
37+
}
38+
39+
return values
40+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# 🔥 Dart || 3 SOlutions || Simple Fast and Easy with Explanation
2+
3+
## Solution - 1 Postorder Traversal - Recursive
4+
5+
Step 1: Traverse the left subtree, i.e., call Preorder(left->subtree)
6+
Step 2: Traverse the right subtree, i.e., call Inorder(right->subtree)
7+
Step 3: Visit the root.
8+
9+
```dart
10+
class Solution {
11+
// Runtime: 513 ms, faster than 18.18% of Dart online submissions for Binary Tree Postorder Traversal.
12+
// Memory Usage: 140.7 MB, less than 36.36% of Dart online submissions for Binary Tree Postorder Traversal.
13+
List<int> postorderTraversal(TreeNode? root) {
14+
List<int> ans = List.empty(growable: true);
15+
solve(root, ans);
16+
return ans;
17+
}
18+
19+
void solve(TreeNode? root, List<int> ans) {
20+
// Base case
21+
if (root == null) return;
22+
23+
// Preorder Traversal = LEFT - RIGHT - ROOT
24+
solve(root.left, ans);
25+
solve(root.right, ans);
26+
ans.add(root.val);
27+
}
28+
}
29+
```
30+
31+
## Solution - 2 Iterative Approach Using Stack
32+
33+
```dart
34+
class Solution {
35+
// Runtime: 462 ms, faster than 63.64% of Dart online submissions for Binary Tree Postorder Traversal.
36+
// Memory Usage: 143.1 MB, less than 18.18% of Dart online submissions for Binary Tree Postorder Traversal.
37+
List<int> postorderTraversal(TreeNode? root) {
38+
// Create an array list to store the solution result...
39+
List<int> sol = List.empty(growable: true);
40+
// Return the solution answer if the tree is empty...
41+
if (root == null) return sol;
42+
// Create an empty stack and push the root node...
43+
Queue<TreeNode?> bag = Queue();
44+
bag.add(root);
45+
// Loop till stack is empty...
46+
while (!bag.isEmpty) {
47+
// set peek a node from the stack...
48+
TreeNode? node = bag.last;
49+
// If the subtrees of that node are null, then pop & store the pop value into solution result...
50+
if (node?.left == null && node?.right == null) {
51+
TreeNode? pop = bag.removeLast();
52+
sol.add(pop!.val);
53+
} else {
54+
// Push the right child of the popped node into the stack...
55+
if (node?.right != null) {
56+
bag.add(node?.right);
57+
node?.right = null;
58+
}
59+
// Push the left child of the popped node into the stack...
60+
if (node?.left != null) {
61+
bag.add(node?.left);
62+
node?.left = null;
63+
}
64+
}
65+
}
66+
return sol; // Return the solution list...
67+
}
68+
}
69+
```
70+
71+
## Solution - 3 Without helper Function
72+
73+
```dart
74+
class Solution {
75+
// Runtime: 465 ms, faster than 63.64% of Dart online submissions for Binary Tree Postorder Traversal.
76+
// Memory Usage: 140.4 MB, less than 63.64% of Dart online submissions for Binary Tree Postorder Traversal.
77+
List<int> list = [];
78+
List<int> postorderTraversal(TreeNode? root) {
79+
if (root == null) return list;
80+
81+
postorderTraversal(root.left);
82+
postorderTraversal(root.right);
83+
list.add(root.val);
84+
return list;
85+
}
86+
}
87+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
5454
- [Remove Nth Node From End of List](RemoveNthNodeFromEndOfList/remove_nth_node_from_end_of_list.dart)
5555
- [Linked List Cycle](LinkedListCycle/linked_list_cycle.dart)
5656
- [Binary Tree Preorder Traversal](BinaryTreePreorderTraversal/binary_tree_preorder_traversal.dart)
57+
- [Binary Tree Postorder Traversal](BinaryTreePostorderTraversal/binary_tree_postorder_traversal.dart)
5758

5859
## Reach me via
5960

0 commit comments

Comments
 (0)