LeetCode 226. Invert Binary Tree

Invert a binary tree.

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

This is to apply recursion.

TreeNode* invertTree(TreeNode* root) {
        if(!root) return NULL;                    // end case.
        TreeNode* left = invertTree(root->left);  // invert the left;
        TreeNode* right = invertTree(root->right); // invert the right;
        root->right = left;                        // swap
        root->left = right;
        return root;        
    }



你可能感兴趣的:(LeetCode 226. Invert Binary Tree)