https://leetcode.com/problems/invert-binary-tree/description
Q. 주어진 tree를 좌우전환 하여라.
Solution.
단순히 node의 자식들을 반대로 연결한다.
TreeNode* invertTreeBT(TreeNode* node)
{
if(node == NULL) return NULL;
TreeNode* leftTemp = node->left;
node->left = invertTreeBT(node->right);
node->right = invertTreeBT(leftTemp);
return node;
}
public:
TreeNode* invertTree(TreeNode* root)
{
return invertTreeBT(root);
}
결과.
'Leetcode > Top 100 Liked' 카테고리의 다른 글
[Binary Tree][Medium] 437. Path Sum III (1) | 2024.03.15 |
---|---|
[Binary Tree][Medium] 230. Kth Smallest Element in a BST (0) | 2024.03.14 |
[Binary Tree][Medium] 114. Flatten Binary Tree to Linked List (0) | 2024.03.14 |
[Binary Tree][Easy] 108. Convert Sorted Array to Binary Search Tree (1) | 2024.03.13 |
[Binary Tree][Easy] 104. Maximum Depth of Binary Tree (0) | 2024.03.12 |