# invert-binary-tree | Nicksxs's Blog
Invert a binary tree 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Goog

# Nicksxs's Blog

What hurts more, the pain of hard work or the pain of regret?

0%

# invert-binary-tree

发表于 2015-06-22 分类于 leetcode 阅读次数： 阅读次数： Disqus：

Invert a binary tree

         4
       /   \
      2     7
     / \   / \
    1   3 6   9

to

         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 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 == NULL) return root;
            TreeNode* temp;
            temp = invertTree(root->left);
            root->left = invertTree(root->right);
            root->right = temp;
            return root;
        }
    };

相关文章

- Reverse Bits
- 34_Search_for_a_Range
- leetcode no.3
- Number of 1 Bits
- Path Sum