Why can't I build a new Tree and add node always to it right?


  • -1
    O

    I was thinking about building a new tree, and keep adding the original tree nodes by a preorder to the new tree's right children. But after I code for this, this won't work.

    void flatten_impl(TreeNode *root, TreeNode *oTree) {
        if (root == NULL) return;
        oTree->right = new TreeNode(root->val);
        oTree = oTree->right;
        flatten_impl(root->left, oTree);
        flatten_impl(root->right, oTree);
    }
    void flatten(TreeNode *root) {
        if (root == NULL) return;
        TreeNode *oTree = new TreeNode(root->val);
        flatten_impl(root->left, oTree);
        flatten_impl(root->right, oTree);
        root = oTree;
    }

Log in to reply
 

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