Last active
November 3, 2020 01:33
-
-
Save ufo22940268/f47aab055404ef266dd10089c63ed6a1 to your computer and use it in GitHub Desktop.
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
//Invert a binary tree. | |
// | |
// Example: | |
// | |
// Input: | |
// | |
// | |
// 4 | |
// / \ | |
// 2 7 | |
// / \ / \ | |
//1 3 6 9 | |
// | |
// Output: | |
// | |
// | |
// 4 | |
// / \ | |
// 7 2 | |
// / \ / \ | |
//9 6 3 1 | |
// | |
// Trivia: | |
//This problem was inspired by this original tweet by Max Howell: | |
// | |
// Google: 90% of our engineers use the software you wrote (Homebrew), but you c | |
//an’t invert a binary tree on a whiteboard so f*** off. | |
// Related Topics Tree | |
// 👍 4095 👎 68 | |
//leetcode submit region begin(Prohibit modification and deletion) | |
function TreeNode(val, left, right) { | |
this.val = (val === undefined ? 0 : val) | |
this.left = (left === undefined ? null : left) | |
this.right = (right === undefined ? null : right) | |
} | |
/** | |
* Definition for a binary tree node. | |
* function TreeNode(val, left, right) { | |
* this.val = (val===undefined ? 0 : val) | |
* this.left = (left===undefined ? null : left) | |
* this.right = (right===undefined ? null : right) | |
* } | |
*/ | |
/** | |
* @param {TreeNode} root | |
* @return {TreeNode} | |
*/ | |
var invertTree = function (root) { | |
if (!root) { | |
return root | |
} | |
let tmp = invertTree(root.left) | |
root.left = invertTree(root.right) | |
root.right = tmp | |
return root | |
} | |
//leetcode submit region end(Prohibit modification and deletion) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment