-
[LeetCode] 226. Invert Binary TreeLeetCode 2021. 10. 26. 11:11728x90
https://leetcode.com/problems/invert-binary-tree/
Invert Binary Tree - 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
Invert Binary Tree - Easy


Invert Binary Tree
기존에 주어진 이진트리에서 모든 왼쪽 자식 노드들과 모든 오른쪽 자식 노드들의 위치를 바꿔줘야한다.
node를 인자로 받아 node를 반환하는 run이라는 메소드를 만들어
해당 node가 NULL이 아닐때,
왼쪽자식을 먼저 저장을 해놓고,
왼쪽자식 = run(오른쪽자식)
오른쪽자식 = run(저장된 왼쪽자식)
으로 해주고, 그 노드를 리턴해주자.
전체 풀이 코드
class Solution { public: TreeNode* invertTree(TreeNode* root) {return run(root);} TreeNode* run(TreeNode* node) { if (node) { TreeNode* left = node->left; node->left = run(node->right); node->right = run(left); } return node; } };'LeetCode' 카테고리의 다른 글
[LeetCode] 809. Expressive Words (0) 2021.10.26 [LeetCode] 890. Find and Replace Pattern (0) 2021.10.26 [LeetCode] 1260. Shift 2D Grid (0) 2021.10.25 [LeetCode] 1417. Reformat The String (0) 2021.10.25 [LeetCode] 1007. Minimum Domino Rotations For Equal Row (0) 2021.10.25