Skip to content

Instantly share code, notes, and snippets.

@jporcelli
Created July 25, 2015 07:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jporcelli/32bdef3ddbb1e4750740 to your computer and use it in GitHub Desktop.
Save jporcelli/32bdef3ddbb1e4750740 to your computer and use it in GitHub Desktop.
https://leetcode.com/problems/invert-binary-tree/ Invert Binary Tree - 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 can’t invert a binary tree on a whiteboard so fuck off.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
if(!root) return NULL;
TreeNode *temp = invertTree(root->right);
root->right = invertTree(root->left);
root->left = temp;
return root;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment