Created
July 18, 2022 17:56
-
-
Save Xmerr/aa15890a843cbbb05ba54555f50ead2a to your computer and use it in GitHub Desktop.
Invert Binary Tree
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {TreeNode} node | |
* @return {TreeNode} | |
*/ | |
const invert = node => { | |
if(!node) { | |
return null; | |
} | |
const temp = node?.left; | |
node.left = node?.right; | |
node.right = temp; | |
node.left = invert(node.left); | |
node.right = invert(node.right); | |
return node; | |
} | |
/** | |
* @param {TreeNode} root | |
* @return {TreeNode} | |
*/ | |
const invertTree = root => { | |
const output = invert(root); | |
return output; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This was for https://leetcode.com/problems/invert-binary-tree/