-
Notifications
You must be signed in to change notification settings - Fork 5
/
invert_binary_tree.dart
96 lines (68 loc) · 1.72 KB
/
invert_binary_tree.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
/*
-* Invert Binary Tree *-
Given the root of a binary tree, invert the tree, and return its root.
Example 1:
Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]
Example 2:
Input: root = [2,1,3]
Output: [2,3,1]
Example 3:
Input: root = []
Output: []
Constraints:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
*/
// Definition for a binary tree node.
import 'dart:collection';
class TreeNode {
int val;
TreeNode? left;
TreeNode? right;
TreeNode([this.val = 0, this.left, this.right]);
}
class A {
// recursive
TreeNode? invertTree(TreeNode? root) {
if (root == null) return null;
TreeNode? tempRight = root.right;
root.right = invertTree(root.left);
root.left = invertTree(tempRight);
return root;
}
}
class B {
// Using Queue
TreeNode? invertTree(TreeNode? root) {
if (root == null) return null;
final Queue<TreeNode> stack = Queue();
stack.add(root);
while (stack.isNotEmpty) {
final TreeNode node = stack.removeLast();
final TreeNode? left = node.left;
node.left = node.right;
node.right = left;
if (node.left != null) stack.add(node.left!);
if (node.right != null) stack.add(node.right!);
}
return root;
}
}
class C {
// Depth First Search
TreeNode? invertTree(TreeNode? root) {
if (root == null) return null;
final Queue<TreeNode> queue = Queue();
queue.add(root);
while (queue.isNotEmpty) {
final TreeNode node = queue.removeFirst();
final TreeNode? left = node.left;
node.left = node.right;
node.right = left;
if (node.left != null) queue.add(node.left!);
if (node.right != null) queue.add(node.right!);
}
return root;
}
}