Skip to content

Commit b144fc3

Browse files
BarklimBarklim
authored andcommitted
add inverted tree example
1 parent 822bc79 commit b144fc3

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

0226-invert-binary-tree.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,26 @@ var invertTree = function(node) {
2121
if (node) [node.left, node.right] = [invertTree(node.right), invertTree(node.left)];
2222
return node;
2323
};
24+
25+
// var invertTree = function(root) {
26+
// if (root === null) return null;
27+
28+
// const node = new TreeNode(root.value);
29+
30+
// node.right = invertTree(root.left);
31+
// node.left = invertTree(root.right);
32+
33+
// return node;
34+
// };
35+
36+
// var invertTree = function(root) {
37+
// if (root == null) return null;
38+
// const queue = new Queue([root]);
39+
// while (!queue.isEmpty()) {
40+
// let node = queue.pop();
41+
// [node.left, node.right] = [node.right, node.left];
42+
// if (node.left != null) queue.push(node.left);
43+
// if (node.right != null) queue.push(node.right);
44+
// }
45+
// return root;
46+
// }

example/6.Tree/0226.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@
1111
* @return {TreeNode}
1212
*/
1313
var invertTree = function(root) {
14-
14+
1515
};
1616

17-
const example1 = invertTree(); // [4,2,7,1,3,6,9] //[ 4,7,2,9,6,3,1]
18-
const example2 = invertTree(); // [2,1,3] // [2,3,1]
19-
const example3 = invertTree(); // [] // []
20-
const example4 = invertTree(); // [1,2,3,4,5,6,7] // [1,3,2,7,6,5,4]
21-
const example5 = invertTree(); // [3,2,1] // [3,1,2]
17+
const testCases = [
18+
{ input: [4,2,7,1,3,6,9], expected: [ 4,7,2,9,6,3,1] },
19+
{ input: [2,1,3], expected: [2,3,1] },
20+
{ input: [], expected: [] },
21+
{ input: [1,2,3,4,5,6,7], expected: [1,3,2,7,6,5,4] },
22+
{ input: [3,2,1], expected: [3,1,2] }
23+
];
2224

23-
console.log(example1);
24-
console.log(example2);
25-
console.log(example3);
26-
console.log(example4);
27-
console.log(example5);
25+
const results = testCases.map(({ input }) => invertTree(createTreeFromArray(input)));
26+
var myTreeStringify = JSON.stringify(results, null, 2);
27+
console.log(myTreeStringify);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy