Hyoseo Lee
Home About Projects Blog Thinking
leetcode

226. Invert Binary Tree

Topic Depth-First Search
Area Data Structures
Summary
using recursion to solve this. flip the left and flip the right and switch left and right. that's all.

Problem

View on LeetCode →

Difficulty: Easy
Tags: Tree, Depth-First Search, Breadth-First Search, Binary Tree

Intuition

it seemed easy, and it was easy.

Approach

using recursion to solve this. flip the left and flip the right and switch left and right. that’s all.

Solution

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
struct TreeNode* invertTree(struct TreeNode* root) {
    if(!root){return NULL;}
    invertTree(root->left);
    invertTree(root->right);
    struct TreeNode* tmp;
    tmp = root->left;
    root->left = root->right;
    root->right = tmp;
    return root;
}

Complexity