Solution
package leetcode.sully.week4;
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 L226 {
public TreeNode invertTree(TreeNode root) {
// javax.swing.tree.TreeNode
// λ°μ΄λ리 νΈλ¦¬: λ
Έλλ§λ€ μμμ΄ 2 μ΄νμΈ νΈλ¦¬
// κ·ΈλκΉ λ°°μ΄μμ 1-2-2-2-2-2... μ΄λ° μμΌλ‘ ꡬλΆνλ©΄ λ¨
// νΈλ¦¬μμλ μλ§ λ
Έλλ³΄λ€ μμ λ°μ΄ν°κ° μΌμͺ½ μμ λ
Έλμ μμΌ νκ³
// μλ§ λ
Έλλ³΄λ€ ν° λ°μ΄ν°κ° μ€λ₯Έμͺ½ μμ λ
Έλμ μμΌ ν¨
// root = [4,2,7,1,3,6,9]
// <4> / (2), [7] / {1}, {3} -> (2) / {6}, {9} -> [7]
// val: int ν
// left, right : TreeNode ν
// if (root.right == null) {
// root.right = new TreeNode(root.val);
// }
//
// if (root.left == null) {
// root.left = new TreeNode(root.val);
// }
// TreeNode val = new TreeNode(root.val);
// TreeNode left = new TreeNode(root.left.val);
// TreeNode right = new TreeNode(root.right.val);
//
// TreeNode treeNode = new TreeNode(val.val, left.right, right.left);
if (root == null) {
return null;
}
TreeNode right = invertTree(root.right);
TreeNode left = invertTree(root.left);
root.left = right;
root.right = left;
return root;
}
}
Leave a comment