매일 조금씩

Leet code (Easy): 226. Invert Binary Tree - Java 본문

알고리즘/Tree

Leet code (Easy): 226. Invert Binary Tree - Java

mezo 2024. 10. 28. 20:39
728x90
반응형

 

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        
        invert(root);

        return root;
    }

    private void invert(TreeNode node){
        if(node != null){
            TreeNode temp = node.left;
            node.left = node.right;
            node.right = temp;
            invert(node.left);
            invert(node.right);
        }
    }
}
728x90
반응형