-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path226_InvertBinaryTree.js
More file actions
127 lines (104 loc) · 2.95 KB
/
Copy path226_InvertBinaryTree.js
File metadata and controls
127 lines (104 loc) · 2.95 KB
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
/**
*
* 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.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
function TreeNode(val, left, right) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
/**
* Definition for a binary tree node.
*/
function TreeNode(val, left, right) {
this.val = (val === undefined ? 0 : val);
this.left = (left === undefined ? null : left);
this.right = (right === undefined ? null : right);
}
/**
* Inverts a binary tree
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if (!root) return null;
// Swap left and right children
const temp = root.left;
root.left = root.right;
root.right = temp;
// Recursively invert subtrees
invertTree(root.left);
invertTree(root.right);
return root;
};
// Helper function to convert tree to array (for visualization)
function treeToArray(root) {
if (!root) return [];
const result = [];
const queue = [root];
while (queue.length) {
const node = queue.shift();
if (node) {
result.push(node.val);
queue.push(node.left);
queue.push(node.right);
} else {
result.push(null);
}
}
// Trim trailing nulls
while (result[result.length - 1] === null) {
result.pop();
}
return result;
}
// Test Case 1
const root1 = new TreeNode(4);
root1.left = new TreeNode(2);
root1.right = new TreeNode(7);
root1.left.left = new TreeNode(1);
root1.left.right = new TreeNode(3);
root1.right.left = new TreeNode(6);
root1.right.right = new TreeNode(9);
console.log("Original Tree (Level Order):", treeToArray(root1));
const inverted1 = invertTree(root1);
console.log("Inverted Tree (Level Order):", treeToArray(inverted1));
// Expected Output: [4,7,2,9,6,3,1]
// Test Case 2
const root2 = new TreeNode(2);
root2.left = new TreeNode(1);
root2.right = new TreeNode(3);
console.log("\nOriginal Tree (Level Order):", treeToArray(root2));
const inverted2 = invertTree(root2);
console.log("Inverted Tree (Level Order):", treeToArray(inverted2));
// Expected Output: [2,3,1]
// Test Case 3 (Empty Tree)
const root3 = null;
console.log("\nOriginal Tree (Level Order):", treeToArray(root3));
const inverted3 = invertTree(root3);
console.log("Inverted Tree (Level Order):", treeToArray(inverted3));
// Expected Output: []