Leetcode 226 Solution

This article provides solution to leetcode question 226 (invert-binary-tree)

https://leetcode.com/problems/invert-binary-tree

Solution

/**
 * 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 NULL;

        TreeNode* next_left = invertTree(root->right);
        TreeNode* next_right = invertTree(root->left);

        root->left = next_left;
        root->right = next_right;

        return root;
    }
};