invert a binary tree
2022年5月1日小于 1 分钟
invert a binary tree
Question
Can you invert a binary tree and get an offer from Google?
Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.
— Max Howell (@mxcl) June 10, 2015
Inverting a node means swapping its left child and right child. You need to apply this to all nodes. As following figure illustrates.
Code
// This is the type for the node
// type Node = null | {
// value: number
// left: Node
// right: Node
// }
/**
* @param {Node} node
* @returns {Node}
*/
function invert(node) {
// your code here
}