Skip to content

Commit 28981cb

Browse files
authored
Merge pull request #1795 from akgmage/dev
add bt invert in java and js
2 parents 523e99f + 7bb1272 commit 28981cb

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

Trees/Binary Trees/invert.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class BinaryTreeNode {
2+
int data;
3+
BinaryTreeNode left, right;
4+
5+
public BinaryTreeNode(int data) {
6+
this.data = data;
7+
this.left = this.right = null;
8+
}
9+
}
10+
11+
public class BinaryTreeInverter {
12+
// Time Complexity: O(n). Space Complexity: O(n).
13+
// Approach: The inverse of an empty tree is an empty tree
14+
// The inverse of a tree with root r, and subtrees right and left is a tree with
15+
// root, whose right subtree is the inverse of left and whose left subtree
16+
// is the inverse of right
17+
public BinaryTreeNode invertTree(BinaryTreeNode root) {
18+
if (root != null) {
19+
root.left = invertTree(root.right);
20+
root.right = invertTree(root.left);
21+
}
22+
return root;
23+
}
24+
25+
// Time Complexity: O(n). Space Complexity: O(1).
26+
// Method2: swap pointers
27+
public BinaryTreeNode invertTree2(BinaryTreeNode root) {
28+
if (root != null) {
29+
// swap the pointers in this node
30+
BinaryTreeNode temp = root.left;
31+
root.left = root.right;
32+
root.right = temp;
33+
34+
invertTree2(root.left);
35+
invertTree2(root.right);
36+
}
37+
return root;
38+
}
39+
40+
// Additional methods or code as needed...
41+
42+
public static void main(String[] args) {
43+
// Example usage:
44+
// Construct a binary tree
45+
BinaryTreeNode root = new BinaryTreeNode(1);
46+
root.left = new BinaryTreeNode(2);
47+
root.right = new BinaryTreeNode(3);
48+
root.left.left = new BinaryTreeNode(4);
49+
root.left.right = new BinaryTreeNode(5);
50+
51+
BinaryTreeInverter inverter = new BinaryTreeInverter();
52+
53+
// Invert the binary tree using the first approach
54+
BinaryTreeNode invertedRoot = inverter.invertTree(root);
55+
56+
// Invert the binary tree using the second approach
57+
BinaryTreeNode invertedRoot2 = inverter.invertTree2(root);
58+
59+
// Additional code for printing or further usage...
60+
}
61+
}

Trees/Binary Trees/invert.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
class BinaryTreeNode {
2+
constructor(data) {
3+
this.data = data;
4+
this.left = this.right = null;
5+
}
6+
}
7+
8+
class BinaryTreeInverter {
9+
// Time Complexity: O(n). Space Complexity: O(n).
10+
// Approach: The inverse of an empty tree is an empty tree
11+
// The inverse of a tree with root r, and subtrees right and left is a tree with
12+
// root, whose right subtree is the inverse of left and whoose left subtree
13+
// is the inverse of right
14+
invertTree(root) {
15+
if (root !== null) {
16+
root.left = this.invertTree(root.right);
17+
root.right = this.invertTree(root.left);
18+
}
19+
return root;
20+
}
21+
22+
// Time Complexity: O(n). Space Complexity: O(1).
23+
// Method2: swap pointers
24+
invertTree2(root) {
25+
if (root !== null) {
26+
// swap the pointers in this node
27+
let temp = root.left;
28+
root.left = root.right;
29+
root.right = temp;
30+
31+
this.invertTree2(root.left);
32+
this.invertTree2(root.right);
33+
}
34+
return root;
35+
}
36+
37+
// Additional methods or code as needed...
38+
39+
// Example usage:
40+
static main() {
41+
// Construct a binary tree
42+
let root = new BinaryTreeNode(1);
43+
root.left = new BinaryTreeNode(2);
44+
root.right = new BinaryTreeNode(3);
45+
root.left.left = new BinaryTreeNode(4);
46+
root.left.right = new BinaryTreeNode(5);
47+
48+
let inverter = new BinaryTreeInverter();
49+
50+
// Invert the binary tree using the first approach
51+
let invertedRoot = inverter.invertTree(root);
52+
53+
// Invert the binary tree using the second approach
54+
let invertedRoot2 = inverter.invertTree2(root);
55+
56+
// Additional code for printing or further usage...
57+
}
58+
}
59+
60+
// Example usage:
61+
BinaryTreeInverter.main();

0 commit comments

Comments
 (0)