function NewNode(val, left, right) {
this.val = val
this.left = left
this.right = right
}
let temp
let g = new NewNode(48, 25, 36)
let f = new NewNode(77, 18, 29)
let e = new NewNode(46, 5, 6)
let d = new NewNode(73, 8, 9)
let c = new NewNode(7, d, e)
let b = new NewNode(4, f, g)
let a = new NewNode(1, b, c)
function invertTree(tree) {
if (typeof tree.left === 'undefined' || typeof tree.right === 'undefined') {
return
}
temp = tree.left
tree.left = tree.right
tree.right = temp
invertTree(tree.right)
invertTree(tree.left)
return tree
}
console.log(invertTree(a))
用js实现一个反转二叉树function NewNode(val, left, right) { this.val = val this.left = left this.right = right}let templet g = new NewNode(48, 25, 36)let f = new NewNode(77, 18, 29)let e = new NewNode(46, 5, 6)let d = new NewNode(73, 8, 9)let c