Merge two BST 's | Binary Search Tree | GFG POTD | Dry Run & Explanation

แชร์
ฝัง
  • เผยแพร่เมื่อ 8 ต.ค. 2024
  • Guyz If you Like the Content
    Plz Consider:
    -Subscribing the Channel
    -Like the video
    -Comment your review
    #Tree
    #BST
    #gfgpotd
    #leetcodepotd
    #algorithm
    #graph
    #datastructure
    #topologicalsort
    #directedgraph
    #graphalgorithm
    #sortalgorithm
    #dfs
    #kahnalgorithm
    #programming
    #coding
    #competitiveprogramming
    #codinginterview
    #interviewprep
    #graphtraversal
    #cp
    #dsa
    #GraphAlgorithms
    #GraphTheory
    #Algorithms
    #GFGPOTD (GeeksForGeeks Problem of the Day)
    #LeetCodePOTD (LeetCode Problem of the Day)
    #CompetitiveProgramming
    #DataStructures
    #SortingAlgorithms
    #DFS (Depth First Search)
    #KahnsAlgorithm
    #CodingChallenge
    #Programming
    #InterviewPrep
    #TechInterview

ความคิดเห็น • 3

  • @Veom03
    @Veom03  8 วันที่ผ่านมา

    If You Like the Video then Plzz Consider Subscribing ☺.
    CODE:
    class Solution {
    public:
    void store_left(Node* root,stack& st){
    Node* curr = root;
    while(curr != NULL){
    st.push(curr);
    curr = curr->left;
    }
    }
    vector merge(Node *root1, Node *root2) {
    stack st1,st2;
    vector ans;
    store_left(root1,st1);
    store_left(root2,st2);
    while(!st1.empty() && !st2.empty()){
    if(st1.top()->data < st2.top()->data){
    Node* curr = st1.top();
    st1.pop();
    ans.push_back(curr->data);
    store_left(curr->right,st1);
    }
    else if(st2.top()->data < st1.top()->data){
    Node* curr = st2.top();
    st2.pop();
    ans.push_back(curr->data);
    store_left(curr->right,st2);
    }
    else{
    Node* curr1 = st1.top();
    Node* curr2 = st2.top();
    st1.pop();
    st2.pop();
    ans.push_back(curr1->data);
    ans.push_back(curr2->data);

    store_left(curr1->right,st1);
    store_left(curr2->right,st2);
    }
    }
    while(!st1.empty()){
    Node* curr1 = st1.top();
    st1.pop();
    ans.push_back(curr1->data);
    store_left(curr1->right,st1);
    }
    while(!st2.empty()){
    Node* curr2 = st2.top();
    st2.pop();
    ans.push_back(curr2->data);
    store_left(curr2->right,st2);
    }
    return ans;
    }
    };

  • @AVALACHAITANYA
    @AVALACHAITANYA 8 วันที่ผ่านมา

    Nice explanation

    • @Veom03
      @Veom03  8 วันที่ผ่านมา +1

      Glad you liked it 😊.