알고리즘 문제 풀이/BFS
226. Invert Binary
가나무마
2021. 7. 5. 22:06
Majority Element - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com

[문제 설명]
노드를 좌우 대칭해서 반환하시오
[처음 생각한 접근 방법]
무지성 노드 교환
class Solution {
public TreeNode invertTree(TreeNode root) {
Queue<TreeNode> q = new LinkedList<>();
if (root == null) {
return null;
}
q.offer(root);
while (!q.isEmpty()) {
TreeNode head = q.poll();
TreeNode temp = head.left;
head.left = head.right;
head.right = temp;
if (head.left != null) {
q.offer(head.left);
}
if (head.right != null) {
q.offer(head.right);
}
}
return root;
}
}