// 思路
// 递归 层序遍历 皆可实现 只是递归的 中序遍历 要注意左子树翻转后变成右子树
// 因此 左中右 遍历右子树时 要改成遍历左子树
class Node {
constructor(value, left, right) {
this.value = value;
this.left = left;
this.right = right;
}
}
let n1 = new Node(1, null, null);
let n2 = new Node(2, null, null);
let n4 = new Node(4, n1, n2);
let n6 = new Node(6, null, null);
let root = new Node(5, n4, n6);
function fn(node) {
if (node == null) return
// 前序遍历 中左右
// 交换左右子树
let t = node.right
node.right = node.left
node.left = t
// 遍历子树
fn(node.left)
fn(node.right)
// 后序遍历 左右中
// fn(node.left)
// fn(node.right)
// let t = node.right
// node.right = node.left
// node.left = t
// 中序遍历 左中右
// fn(node.left)
// let t = node.right
// node.right = node.left
// node.left = t
// fn(node.left)
return node
}
// 验证
console.log('翻转结果', fn(root))
console