728x90
226. Invert Binary Tree
-
[LeetCode] 226. Invert Binary Tree c++Coding Test/LeetCode 2022. 12. 19. 19:37
문제 Given the root of a binary tree, invert the tree, and return its root. Input: root = [4,2,7,1,3,6,9] Output: [4,7,2,9,6,3,1] 부모 노드를 기준으로 자식 노드의 좌우 대칭을 바꾸는 문제입니다. 문제 해결 class Solution { public: TreeNode* invertTree(TreeNode* root) { if (!root) return root; swap(root->left, root->right); invertTree(root->left); invertTree(root->right); return root; } }; swap()함수는 algorithm에 내장된 STL함수입니다. swap()..