2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d8259367417b2b2512c83
|
2021-12-19 21:02:25 +05:30
|
|
|
title: 反转二叉树
|
2018-10-10 18:03:03 -04:00
|
|
|
challengeType: 1
|
2021-02-06 04:42:36 +00:00
|
|
|
forumTopicId: 301704
|
2021-01-13 03:31:00 +01:00
|
|
|
dashedName: invert-a-binary-tree
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --description--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-12-19 21:02:25 +05:30
|
|
|
这里我们将创建一个反转二叉树的函数。 给定二叉树,我们希望生成一个新树,它等效于该树的镜像。 与原始树的中序遍历相比,在倒转树上运行中序遍历将以相反的顺序探索节点。 在我们的二叉树上编写一个名为 `invert` 的方法。 调用此方法应该反转当前树结构。 理想情况下,我们希望在线性时间内就地执行此操作。 也就是说,我们只访问每个节点一次,我们在不使用任何额外内存的情况下修改现有的树结构。 祝你好运!
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --hints--
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2021-12-19 21:02:25 +05:30
|
|
|
存在 `BinarySearchTree` 数据结构。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
```js
|
|
|
|
assert(
|
|
|
|
(function () {
|
|
|
|
var test = false;
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
}
|
|
|
|
return typeof test == 'object';
|
|
|
|
})()
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-12-19 21:02:25 +05:30
|
|
|
二叉搜索树有一个名为 `invert` 的方法。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
assert(
|
|
|
|
(function () {
|
|
|
|
var test = false;
|
|
|
|
if (typeof BinarySearchTree !== 'undefined') {
|
|
|
|
test = new BinarySearchTree();
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return typeof test.invert == 'function';
|
|
|
|
})()
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-12-19 21:02:25 +05:30
|
|
|
`invert` 方法正确地反转树结构。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
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';
|
|
|
|
})()
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
2021-12-19 21:02:25 +05:30
|
|
|
反转空树应该返回 `null`。
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
```js
|
2020-12-16 00:37:30 -07:00
|
|
|
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;
|
|
|
|
})()
|
|
|
|
);
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-08-13 17:24:35 +02:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
# --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
|
2021-02-06 04:42:36 +00:00
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
// Only change code above this line
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-12-16 00:37:30 -07:00
|
|
|
# --solutions--
|
|
|
|
|
2021-01-13 03:31:00 +01:00
|
|
|
```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
|
|
|
|
}
|
|
|
|
```
|