ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [leetcode] 897. Increasing Order Search Tree
    카테고리 없음 2021. 3. 20. 11:54
    728x90
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        TreeNode* answer = new TreeNode(-1);
        TreeNode* dummy = answer;
        TreeNode* increasingBST(TreeNode* root) {
            if(!root) return nullptr;
            
            increasingBST(root->left);
            
            TreeNode* tmp = new TreeNode(root->val);
            answer->right = tmp;
            answer = answer->right;
            
            increasingBST(root->right);
            
            return dummy->right;
        }
    };

     

    Inorder Traversal을 쭉 돌리면 되는 간단한 문제

    시간복잡도: O(n)

Designed by Tistory.