c++ solution (recursion, preOrder, 6ms)


  • 0
    class Solution {
    public:
        void preOrder(TreeNode* root, TreeNode* &prev) {
            if (!root) return;
            TreeNode* tmp = root->right;
            prev->right = root;
            prev = root;
            preOrder(root->left, prev);
            root->left = NULL;
            preOrder(tmp, prev);
        }
        void flatten(TreeNode* root) {
            if (!root) return;
            TreeNode* tmp = root->right, *prev = root;
            preOrder(root->left, prev);
            root->left = NULL;
            preOrder(tmp, prev);
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.