|
| 1 | +/** |
| 2 | + * Definition for a binary tree node. |
| 3 | + * function TreeNode(val, left, right) { |
| 4 | + * this.val = (val===undefined ? 0 : val) |
| 5 | + * this.left = (left===undefined ? null : left) |
| 6 | + * this.right = (right===undefined ? null : right) |
| 7 | + * } |
| 8 | + */ |
| 9 | +/** |
| 10 | + * @param {TreeNode} root |
| 11 | + * @return {TreeNode} |
| 12 | + */ |
| 13 | + |
| 14 | +// ✔️ Recursive Approach |
| 15 | +// Time Complexity: O(N), N = Total number of nodes (each node is processed once) |
| 16 | +// Space Complexity: O(H), H = Height of the tree (due to recursion stack depth) |
| 17 | + |
| 18 | +var invertTree = function (root) { |
| 19 | + if (!root) return null; |
| 20 | + |
| 21 | + [root.left, root.right] = [root.right, root.left]; |
| 22 | + |
| 23 | + invertTree(root.left); |
| 24 | + invertTree(root.right); |
| 25 | + |
| 26 | + return root; |
| 27 | +}; |
| 28 | + |
| 29 | +/** |
| 30 | + * Definition for a binary tree node. |
| 31 | + * function TreeNode(val, left, right) { |
| 32 | + * this.val = (val===undefined ? 0 : val) |
| 33 | + * this.left = (left===undefined ? null : left) |
| 34 | + * this.right = (right===undefined ? null : right) |
| 35 | + * } |
| 36 | + */ |
| 37 | +/** |
| 38 | + * @param {TreeNode} root |
| 39 | + * @return {TreeNode} |
| 40 | + */ |
| 41 | + |
| 42 | +// ✔️ Stack → DFS approach |
| 43 | +// Time Complexity: O(N), N = Total number of nodes (each node is processed once) |
| 44 | +// Space Complexity: O(H), H = Height of the tree (due to recursion stack depth) |
| 45 | + |
| 46 | +// var invertTree = function (root) { |
| 47 | +// let stack = [root]; |
| 48 | + |
| 49 | +// while (stack.length > 0) { |
| 50 | +// const node = stack.pop(); |
| 51 | +// if (!node) continue; |
| 52 | + |
| 53 | +// [node.left, node.right] = [node.right, node.left]; |
| 54 | +// stack.push(node.left); |
| 55 | +// stack.push(node.right); |
| 56 | +// } |
| 57 | +// return root; |
| 58 | +// }; |
| 59 | + |
| 60 | + |
| 61 | +/** |
| 62 | + * Definition for a binary tree node. |
| 63 | + * function TreeNode(val, left, right) { |
| 64 | + * this.val = (val===undefined ? 0 : val) |
| 65 | + * this.left = (left===undefined ? null : left) |
| 66 | + * this.right = (right===undefined ? null : right) |
| 67 | + * } |
| 68 | + */ |
| 69 | +/** |
| 70 | + * @param {TreeNode} root |
| 71 | + * @return {TreeNode} |
| 72 | + */ |
| 73 | + |
| 74 | +// ✔️ Queue → BFS |
| 75 | +// Time Complexity: O(N), N = Total number of nodes (each node is processed once) |
| 76 | +// Space Complexity: O(W), W = Maximum width of the tree |
| 77 | +// var invertTree = function (root) { |
| 78 | +// let queue = [root]; |
| 79 | + |
| 80 | +// while (queue.length > 0) { |
| 81 | +// const node = queue.shift(); |
| 82 | +// if (!node) continue; |
| 83 | + |
| 84 | +// [node.left, node.right] = [node.right, node.left]; |
| 85 | +// queue.push(node.left); |
| 86 | +// queue.push(node.right); |
| 87 | +// } |
| 88 | +// return root; |
| 89 | +// }; |
| 90 | + |
| 91 | + |
0 commit comments