Skip to content

Instantly share code, notes, and snippets.

@Leask
Created June 12, 2015 18:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Leask/1f0dbfbf95485b20ed1e to your computer and use it in GitHub Desktop.
Save Leask/1f0dbfbf95485b20ed1e to your computer and use it in GitHub Desktop.
[Invert Binary Tree] 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. https://leetcode.com/problems/invert-binary-tree/#.VXqvIdRFNy8.twitter
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if (root) {
var left = root.left ? root.left : null,
right = root.right ? root.right : null;
root.left = invertTree(right);
root.right = invertTree(left);
}
return root;
};
@op1ekun
Copy link

op1ekun commented Oct 13, 2016

You know that's an algorithm for reversing (creating a mirror of a tree), right?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment