반응형
https://leetcode.com/problems/increasing-order-search-tree/
중위 순회로 노드를 방문하면서 재배열된 트리를 만들어주었습니다.
class Solution {
public:
TreeNode* newRoot = NULL;
TreeNode* cur = NULL;
TreeNode* increasingBST(TreeNode* root) {
inorder(root);
return newRoot;
}
void inorder(TreeNode* node) {
if(!node) return;
inorder(node->left);
if(!newRoot) cur = newRoot = node;
else {
node->left = NULL;
cur = cur->right = node;
}
inorder(node->right);
}
};
반응형
'Algorithm' 카테고리의 다른 글
LeetCode 173 : Binary Search Tree Iterator (0) | 2022.04.20 |
---|---|
LeetCode 230 : Kth Smallest Element in a BST (0) | 2022.04.18 |
LeetCode 448 : Find All Numbers Disappeared in an Array (0) | 2022.04.16 |
LeetCode 538 : Convert BST to Greater Tree (0) | 2022.04.16 |
LeetCode 700 : Search in a Binary Search Tree (0) | 2022.04.14 |