226 - Invert Binary Tree
info
- 문제 보기: 226 - Invert Binary Tree
- 소요 시간: 11분 38초
- 풀이 언어:
java
- 체감 난이도: 2️⃣
- 리뷰 횟수: ✅
풀이 키워드
스포주의
dfs
풀이 코드
info
- 메모리: 41660 KB
- 시간: 0 ms
/**
* 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) {
if (root == null) return null;
invertTree(root.left);
invertTree(root.right);
TreeNode tmp = root.right;
root.right = root.left;
root.left = tmp;
return root;
}
}
풀이 해설
WIP
메모
- 읽지만은 말고 한번씩 따라쳐보면 좋을 문제
- 만약 배열로 주어졌다면?