create a tree with only right elements from a binary tree

Viewed 13

link to the problem : https://leetcode.com/problems/increasing-order-search-tree/

TreeNode* res_root = NULL, *temp;

void BST_Create(TreeNode* root)
{
    if(root == NULL)
        return;
    
    BST_Create(root->left);
    
    if(res_root == NULL)
    {
        temp = new TreeNode(root->val);
        res_root = temp;
        //temp = temp->right;
    }
    else{
          temp->right = new TreeNode(root->val);
       // temp = new TreeNode(root->val);
       // temp = temp->right;
    }
    
    BST_Create(root->right);
}

TreeNode* increasingBST(TreeNode* root) {
    if(root==NULL)
        return NULL;
    
    BST_Create(root);
    return res_root;
} 

the commented out code was my earlier code , with the output i was able to figure out that connection between my nodes was not happening with my earlier code since it returned only one node but failed to understand why .

help me understand why it happened .

0 Answers
Related