--- id: 587d8259367417b2b2512c83 title: 二分木を反転させる challengeType: 1 forumTopicId: 301704 dashedName: invert-a-binary-tree --- # --description-- ここでは、二分木を反転させる関数を作成します。 与えられた二分木の左右を逆にして新しい木を作りましょう。 反転した木で通りがけ順走査を実行すると、元の木の通りがけ順走査とは逆の順にノードが探索されます。 これを行うための `invert` と呼ばれるメソッドを二分木に対して記述してください。 このメソッドを呼び出すことで現在のツリー構造が反転する必要があります。 線形時間計算量でこれをそのまま行えるのが理想です。 つまり、各ノードを 1 回訪れ、追加のメモリを使用せずに、走査しながら既存のツリー構造を変更します。 頑張ってください! # --hints-- `BinarySearchTree` データ構造が存在する必要があります。 ```js assert( (function () { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree(); } return typeof test == 'object'; })() ); ``` 二分探索木に `invert` というメソッドが必要です。 ```js assert( (function () { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree(); } else { return false; } return typeof test.invert == 'function'; })() ); ``` `invert` メソッドはツリー構造を正しく反転させる必要があります。 ```js assert( (function () { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree(); } else { return false; } if (typeof test.invert !== 'function') { return false; } test.add(4); test.add(1); test.add(7); test.add(87); test.add(34); test.add(45); test.add(73); test.add(8); test.invert(); return test.inorder().join('') == '877345348741'; })() ); ``` 空の木を反転させると、`null` が返される必要があります。 ```js assert( (function () { var test = false; if (typeof BinarySearchTree !== 'undefined') { test = new BinarySearchTree(); } else { return false; } if (typeof test.invert !== 'function') { return false; } return test.invert() == null; })() ); ``` # --seed-- ## --after-user-code-- ```js BinarySearchTree.prototype = Object.assign( BinarySearchTree.prototype, { add: function(value) { function searchTree(node) { if (value < node.value) { if (node.left == null) { node.left = new Node(value); return; } else if (node.left != null) { return searchTree(node.left) }; } else if (value > node.value) { if (node.right == null) { node.right = new Node(value); return; } else if (node.right != null) { return searchTree(node.right); }; } else { return null; }; } var node = this.root; if (node == null) { this.root = new Node(value); return; } else { return searchTree(node); }; }, inorder: function() { if (this.root == null) { return null; } else { var result = new Array(); function traverseInOrder(node) { if (node.left != null) { traverseInOrder(node.left); }; result.push(node.value); if (node.right != null) { traverseInOrder(node.right); }; } traverseInOrder(this.root); return result; }; } } ); ``` ## --seed-contents-- ```js var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2)); function Node(value) { this.value = value; this.left = null; this.right = null; } function BinarySearchTree() { this.root = null; // Only change code below this line // Only change code above this line } ``` # --solutions-- ```js var displayTree = (tree) => console.log(JSON.stringify(tree, null, 2)); function Node(value) { this.value = value; this.left = null; this.right = null; } function BinarySearchTree() { this.root = null; // Only change code below this line this.invert = function(node = this.root) { if (node) { const temp = node.left; node.left = node.right; node.right = temp; this.invert(node.left); this.invert(node.right); } return node; } // Only change code above this line } ```