[Leetcode] 226. Invert Binary Tree

Algorithm|2021. 9. 9. 19:10
반응형

Identifying the Problem

모든 이진트리를 뒤집는다.

Organizing thoughts

Sourcecode

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) 
    {
        if(!root) return NULL; //탈출 조건
        
        swap(root->left, root->right); //왼쪽과 오른쪽 node를 뒤집음
        
        invertTree(root->left); //재귀 탐색
        invertTree(root->right);
            
        return root;
    }
};

 

반응형

댓글()