Skip to content

Commit 40b1a6e

Browse files
committed
leetcode
1 parent ac18394 commit 40b1a6e

File tree

4 files changed

+202
-0
lines changed

4 files changed

+202
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
3+
4+
-* Invert Binary Tree *-
5+
6+
Given the root of a binary tree, invert the tree, and return its root.
7+
8+
9+
10+
Example 1:
11+
12+
13+
Input: root = [4,2,7,1,3,6,9]
14+
Output: [4,7,2,9,6,3,1]
15+
Example 2:
16+
17+
18+
Input: root = [2,1,3]
19+
Output: [2,3,1]
20+
Example 3:
21+
22+
Input: root = []
23+
Output: []
24+
25+
26+
Constraints:
27+
28+
The number of nodes in the tree is in the range [0, 100].
29+
-100 <= Node.val <= 100
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 A {
44+
// recursive
45+
TreeNode? invertTree(TreeNode? root) {
46+
if (root == null) return null;
47+
TreeNode? tempRight = root.right;
48+
root.right = invertTree(root.left);
49+
root.left = invertTree(tempRight);
50+
return root;
51+
}
52+
}
53+
54+
class B {
55+
// Using Queue
56+
TreeNode? invertTree(TreeNode? root) {
57+
if (root == null) return null;
58+
59+
final Queue<TreeNode> stack = Queue();
60+
stack.add(root);
61+
62+
while (stack.isNotEmpty) {
63+
final TreeNode node = stack.removeLast();
64+
final TreeNode? left = node.left;
65+
node.left = node.right;
66+
node.right = left;
67+
68+
if (node.left != null) stack.add(node.left!);
69+
70+
if (node.right != null) stack.add(node.right!);
71+
}
72+
return root;
73+
}
74+
}
75+
76+
class C {
77+
// Depth First Search
78+
TreeNode? invertTree(TreeNode? root) {
79+
if (root == null) return null;
80+
81+
final Queue<TreeNode> queue = Queue();
82+
queue.add(root);
83+
84+
while (queue.isNotEmpty) {
85+
final TreeNode node = queue.removeFirst();
86+
final TreeNode? left = node.left;
87+
node.left = node.right;
88+
node.right = left;
89+
90+
if (node.left != null) queue.add(node.left!);
91+
92+
if (node.right != null) queue.add(node.right!);
93+
}
94+
return root;
95+
}
96+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
// Definition for a binary tree node.
4+
type TreeNode struct {
5+
Val int
6+
Left *TreeNode
7+
Right *TreeNode
8+
}
9+
10+
func invertTree(root *TreeNode) *TreeNode {
11+
stack := []*TreeNode{root}
12+
13+
for len(stack) != 0 {
14+
pop := stack[len(stack)-1]
15+
stack = stack[:len(stack)-1]
16+
17+
if pop != nil {
18+
pop.Right, pop.Left = pop.Left, pop.Right
19+
stack = append(stack, pop.Right, pop.Left)
20+
}
21+
22+
}
23+
24+
return root
25+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
# 🔥 Invert Binary Tree 🔥 || 3 Solutions || Simple Fast and 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
15+
16+
```dart
17+
class Solution {
18+
TreeNode? invertTree(TreeNode? root) {
19+
if (root == null) return null;
20+
TreeNode? tempRight = root.right;
21+
root.right = invertTree(root.left);
22+
root.left = invertTree(tempRight);
23+
return root;
24+
}
25+
}
26+
```
27+
28+
## Solution - 2 Using Queue as Stack
29+
30+
```dart
31+
import 'dart:collection';
32+
33+
class Solution {
34+
TreeNode? invertTree(TreeNode? root) {
35+
if (root == null) return null;
36+
37+
final Queue<TreeNode> stack = Queue();
38+
stack.add(root);
39+
40+
while (stack.isNotEmpty) {
41+
final TreeNode node = stack.removeLast();
42+
final TreeNode? left = node.left;
43+
node.left = node.right;
44+
node.right = left;
45+
46+
if (node.left != null) stack.add(node.left!);
47+
48+
if (node.right != null) stack.add(node.right!);
49+
}
50+
return root;
51+
}
52+
}
53+
```
54+
55+
## Solution - 3 BFS (Level Order Traversal)
56+
57+
```dart
58+
import 'dart:collection';
59+
60+
class Solution {
61+
TreeNode? invertTree(TreeNode? root) {
62+
if (root == null) return null;
63+
64+
final Queue<TreeNode> queue = Queue();
65+
queue.add(root);
66+
67+
while (queue.isNotEmpty) {
68+
final TreeNode node = queue.removeFirst();
69+
final TreeNode? left = node.left;
70+
node.left = node.right;
71+
node.right = left;
72+
73+
if (node.left != null) queue.add(node.left!);
74+
75+
if (node.right != null) queue.add(node.right!);
76+
}
77+
return root;
78+
}
79+
}
80+
```

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
7979
- [Contains Duplicate](ContainsDuplicate/contains_duplicate.dart)
8080
- [Contains Duplicate II](ContainsDuplicate-II/contains_duplicate_II.dart)
8181
- [Implement Stack using Queues](ImplementStackUsingQueues/implement_stack_using_queues.dart)
82+
- [Invert Binary Tree](InvertBinaryTree/invert_binary_tree.dart)
8283

8384
## Reach me via
8485

0 commit comments

Comments
 (0)