Halo World

[LeetCode] 226. Invert Binary Tree 본문

스터디/알고리즘 문제풀이

[LeetCode] 226. Invert Binary Tree

_Yeony 2021. 10. 4. 10:20

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

class Solution {
    public TreeNode invertTree(TreeNode root) {
        TreeNode head= root;
        
        if(root!=null) invert(root);
        return head;
    }
    
    private void invert(TreeNode n1) {
        if(n1==null) return;  

        invert(n1.right);
        invert(n1.left);
        TreeNode tmp = n1.right;
        n1.right = n1.left;
        n1.left = tmp;
    }
}