Leetcode June Challenge - Day 1

Problem: LeetCode June Challenge - Day 1

Ideas:

  1. The problem is really simple, you just need to swap left and right for every node, as a tree is inverted iff every subtree is inverted.

Solution:

    class Solution {
    public:
        TreeNode* invertTree(TreeNode* root) {
            if(root == NULL) return NULL;
            invertTree(root->left);
            invertTree(root->right);
            swap(root->left, root->right);
            return root;
        }
    };