< 문제 간단설명 >
주어진 트리를 좌우반전시켜 반환하는 문제이다.
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {TreeNode}
*/
var invertTree = function(root) {
if(root === null) {
return root;
}
invertTree(root.left);
invertTree(root.right);
[root.left, root.right] = [root.right, root.left];
return root;
};
'PS > LeetCode' 카테고리의 다른 글
LeetCode / Tree / 543번 / Diameter of Binary Tree / JS (0) | 2023.04.28 |
---|---|
LeetCode / Tree / 110번 / Balanced Binary Tree / JS (0) | 2023.04.28 |
LeetCode / Greedy / 621번 / Task Scheduler / JS (0) | 2023.04.28 |
LeetCode / Greedy / 2131번 / Longest Palindrome by Concatenating Two Letter Words / JS (0) | 2023.04.28 |