Skip to content

Commit 90e7585

Browse files
committed
leetcode
1 parent b30ac4f commit 90e7585

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
3+
-* Binary Tree Pre-Order Traversal *-
4+
5+
Given the root of a binary tree, return the preorder traversal of its nodes' values.
6+
7+
8+
9+
Example 1:
10+
11+
12+
Input: root = [1,null,2,3]
13+
Output: [1,2,3]
14+
Example 2:
15+
16+
Input: root = []
17+
Output: []
18+
Example 3:
19+
20+
Input: root = [1]
21+
Output: [1]
22+
23+
24+
Constraints:
25+
26+
The number of nodes in the tree is in the range [0, 100].
27+
-100 <= Node.val <= 100
28+
29+
30+
Follow up: Recursive solution is trivial, could you do it iteratively?
31+
32+
33+
34+
*/
35+
36+
// Definition for a binary tree node.
37+
import 'dart:collection';
38+
39+
class TreeNode {
40+
int val;
41+
TreeNode? left;
42+
TreeNode? right;
43+
TreeNode([this.val = 0, this.left, this.right]);
44+
}
45+
46+
class A {
47+
// Recursion
48+
// Runtime: 448 ms, faster than 77.78% of Dart online submissions for Binary Tree Preorder Traversal.
49+
// Memory Usage: 140.1 MB, less than 88.89% of Dart online submissions for Binary Tree Preorder Traversal.
50+
List<int> preorderTraversal(TreeNode? root) {
51+
List<int> list = List.empty(growable: true);
52+
preOrder(root, list);
53+
return list;
54+
}
55+
56+
void preOrder(TreeNode? root, List<int> list) {
57+
if (root == null) return;
58+
list.add(root.val);
59+
preOrder(root.left, list);
60+
preOrder(root.right, list);
61+
}
62+
}
63+
64+
class B {
65+
// Iterative Approach Using Stack
66+
// Runtime: 503 ms, faster than 44.44% of Dart online submissions for Binary Tree Preorder Traversal.
67+
// Memory Usage: 142.7 MB, less than 16.67% of Dart online submissions for Binary Tree Preorder Traversal.
68+
List<int> preorderTraversal(TreeNode? root) {
69+
// Create an array list to store the solution result...
70+
List<int> sol = List.empty(growable: true);
71+
// Return the solution answer if the tree is empty...
72+
if (root == null) return sol;
73+
// Create an empty stack and push the root node...
74+
Queue<TreeNode?> bag = Queue();
75+
bag.add(root);
76+
// Loop till stack is empty...
77+
while (!bag.isEmpty) {
78+
// Pop a node from the stack...
79+
TreeNode? node = bag.removeLast();
80+
sol.add(node!.val);
81+
// Push the right child of the popped node into the stack...
82+
if (node.right != null) bag.add(node.right);
83+
// Push the left child of the popped node into the stack...
84+
if (node.left != null) bag.add(node.left);
85+
}
86+
return sol; // Return the solution list...
87+
}
88+
}
89+
90+
class C {
91+
// Runtime: 418 ms, faster than 94.44% of Dart online submissions for Binary Tree Preorder Traversal.
92+
// Memory Usage: 140.6 MB, less than 16.67% of Dart online submissions for Binary Tree Preorder Traversal.
93+
List<int> preorder = List.empty(growable: true);
94+
List<int> preorderTraversal(TreeNode? root) {
95+
if (root == null) return preorder;
96+
97+
preorder.add(root.val);
98+
preorderTraversal(root.left);
99+
preorderTraversal(root.right);
100+
101+
return preorder;
102+
}
103+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package main
2+
3+
type TreeNode struct {
4+
Val int
5+
Left *TreeNode
6+
Right *TreeNode
7+
}
8+
9+
/*
10+
11+
Runtime: 0 ms, faster than 100.00% of Go online submissions for Binary Tree Preorder Traversal.
12+
Memory Usage: 2.1 MB, less than 40.85% of Go online submissions for Binary Tree Preorder Traversal.
13+
14+
*/
15+
16+
func preorderTraversal(root *TreeNode) []int {
17+
var res []int
18+
helper(root, &res)
19+
return res
20+
}
21+
22+
func helper(root *TreeNode, res *[]int) {
23+
if root == nil {
24+
return
25+
}
26+
*res = append(*res, root.Val)
27+
helper(root.Left, res)
28+
helper(root.Right, res)
29+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# 🔥 Dart || 3 Solution || Simple Fast Easy with Explanation
2+
3+
## Definition for a binary tree node
4+
5+
```dart
6+
class TreeNode {
7+
int val;
8+
TreeNode? left;
9+
TreeNode? right;
10+
TreeNode([this.val = 0, this.left, this.right]);
11+
}
12+
```
13+
14+
## Solution - 1 Recursive with helper Function
15+
16+
```dart
17+
class Solution {
18+
// Runtime: 448 ms, faster than 77.78% of Dart online submissions for Binary Tree Preorder Traversal.
19+
// Memory Usage: 140.1 MB, less than 88.89% of Dart online submissions for Binary Tree Preorder Traversal.
20+
List<int> preorderTraversal(TreeNode? root) {
21+
List<int> list = List.empty(growable: true);
22+
preOrder(root, list);
23+
return list;
24+
}
25+
26+
void preOrder(TreeNode? root, List<int> list) {
27+
if (root == null) return;
28+
list.add(root.val);
29+
preOrder(root.left, list);
30+
preOrder(root.right, list);
31+
}
32+
}
33+
```
34+
35+
## Solution - 2 Recursive without Helper Function
36+
37+
```dart
38+
class Solution {
39+
// Runtime: 418 ms, faster than 94.44% of Dart online submissions for Binary Tree Preorder Traversal.
40+
// Memory Usage: 140.6 MB, less than 16.67% of Dart online submissions for Binary Tree Preorder Traversal.
41+
42+
// Global list to hold our values
43+
List<int> preorder = List.empty(growable: true);
44+
List<int> preorderTraversal(TreeNode? root) {
45+
// if tree-node is empty than we will return empty list
46+
if (root == null) return preorder;
47+
// else we will add the value into the list
48+
preorder.add(root.val);
49+
// adding the value from left side
50+
preorderTraversal(root.left);
51+
// adding OR arranging value from right side
52+
preorderTraversal(root.right);
53+
54+
return preorder;
55+
}
56+
}
57+
```
58+
59+
## Solution - 3 Iterative Approach Using Stack
60+
61+
I use Queue as stack not ListStack version which i don't like because I can manipulate Queue just like stack and it work on both sides
62+
63+
```dart
64+
65+
import 'dart:collection';
66+
67+
class Solution {
68+
// Runtime: 503 ms, faster than 44.44% of Dart online submissions for Binary Tree Preorder Traversal.
69+
// Memory Usage: 142.7 MB, less than 16.67% of Dart online submissions for Binary Tree Preorder Traversal.
70+
List<int> preorderTraversal(TreeNode? root) {
71+
// Create an array list to store the solution result...
72+
List<int> sol = List.empty(growable: true);
73+
// Return the solution answer if the tree is empty...
74+
if (root == null) return sol;
75+
// Create an empty stack and push the root node...
76+
Queue<TreeNode?> bag = Queue();
77+
bag.add(root);
78+
// Loop till stack is empty...
79+
while (!bag.isEmpty) {
80+
// Pop a node from the stack...
81+
TreeNode? node = bag.removeLast();
82+
sol.add(node!.val);
83+
// Push the right child of the popped node into the stack...
84+
if (node.right != null) bag.add(node.right);
85+
// Push the left child of the popped node into the stack...
86+
if (node.left != null) bag.add(node.left);
87+
}
88+
return sol; // Return the solution list...
89+
}
90+
}
91+
92+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
5353
- [Single Number](SingleNumber/single_number.dart)
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)
56+
- [Binary Tree Preorder Traversal](BinaryTreePreorderTraversal/binary_tree_preorder_traversal.dart)
5657

5758
## Reach me via
5859

0 commit comments

Comments
 (0)