Invert Binary Tree

原题:
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.

解题:
这个题还是比较简单的,依次递归交换左右子树即可。AC的C++代码如下:

    TreeNode* invertTree(TreeNode* root) {
      if(!root || (!root->left && !root->right))
          return root;
      TreeNode* temp = NULL;
      temp = root->left;
      root->left = root->right;
      root->right = temp;

      invertTree(root->left);
      invertTree(root->right);

      return root;
    }

你可能感兴趣的:(LeetCode,C++,treenode)