-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathbinary_tree_inorder_trasversal.dart
136 lines (105 loc) · 3.23 KB
/
binary_tree_inorder_trasversal.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
-* Binary Tree Inorder Traversal *-
Given the root of a binary tree, return the inorder traversal of its nodes' values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,3,2]
Example 2:
Input: root = []
Output: []
Example 3:
Input: root = [1]
Output: [1]
Constraints:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
*/
/*
This is one of the basic Tree questions. Since, it is a tree question, we can safely think the solution could be RECURSIVE.
Since, we are supposed to return a vector of integers, I declared that as the first line in my code.
Later, if the base condition, which is, if root == NULL it shall return the empty vector.
Else, we populate the initialized vector with left children of the current root, by calling the same function on Left child.
Then, we just push_back the root->val into the vector.
Later, we should call for root->right to insert the Right children.
By the way,
InOrder = Left, root, Right
PreOrder = root, Left, Right
PostOrder = Left, Right, root
Functions used in the code:
if only one data value: vector_name.insert( position_to_insert, data )
if another vector: vector_name.insert( position_to_insert , vector2_name.begin(),vector2_name.end() )
vector2_name is inserted at the desired location in vector_name. for this question it is vector_name.end().
vector_name.begin() -> returns a pointer where the vector starts
vector_name.end() -> returns a pointer where the vector ends
*/
//- Definition for a binary tree node.
class TreeNode {
int val;
TreeNode? left;
TreeNode? right;
TreeNode([this.val = 0, this.left, this.right]);
}
// Recursive
class A {
// 70 / 70 test cases passed, but took too long.
List<int> inorderTraversal(TreeNode? root) {
List<int> res = <int>[];
if (root == null) return res;
helper(root, res);
return res;
}
void helper(TreeNode? root, List<int> res) {
if (root == null) return;
helper(root.left, res);
res.add(root.val);
helper(root.right, res);
}
}
// Stack
class B {
List<int> inorderTraversal(TreeNode? root) {
List stack = [];
List<int> ans = [];
while (root != null || stack.length != 0) {
while (root != null) {
stack.add(root);
root = root.left;
}
root = stack.removeLast();
ans.add(root!.val);
root = root.right;
}
return ans;
}
// Runtime: 372 ms, faster than 80.00% of Dart online submissions for Binary Tree Inorder Traversal.
// Memory Usage: 140.2 MB, less than 86.67% of Dart online submissions for Binary Tree Inorder Traversal.
}
class C {
// 70 / 70 test cases passed, but took too long.
List<int> inorderTraversal(TreeNode? root) {
List<int> ans = <int>[];
inOrder(root, ans);
return ans;
}
void inOrder(TreeNode? root, List<int> ans) {
while (root != null) {
if (root.left == null) {
ans.add(root.val);
root = root.right;
} else {
TreeNode? temp = getPreOrderSuccessor(root);
temp.right = root;
TreeNode? left = root.left;
root.left = null;
root = left!;
}
}
}
TreeNode getPreOrderSuccessor(TreeNode root) {
TreeNode? node = root.left;
while (node!.right != null) {
node = node.right;
}
return node;
}
}