L49. Inorder Successor/Predecessor in BST | 3 Methods

แชร์
ฝัง
  • เผยแพร่เมื่อ 9 พ.ย. 2024

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

  • @takeUforward
    @takeUforward  3 ปีที่แล้ว +55

    Please like and share ^ _ ^, also comment down the asked question's answer below!!

    • @shashankbajpai5659
      @shashankbajpai5659 2 ปีที่แล้ว +4

      please Likeeeeeeee and subcribeeeeeee likhna tha

    • @priyanshkumar17
      @priyanshkumar17 8 หลายเดือนก่อน

      Thanks Striver!! :))

  • @nitinkumarsingh7959
    @nitinkumarsingh7959 3 ปีที่แล้ว +202

    In 3rd method the basic idea is same as finding ceil of a node in bst. The only change here is that the ceil value must be just greater than the node and rest everything is same. And the predessor one is same as findind floor value which is not equal to and just less than.

  • @atharvakulkarni2024
    @atharvakulkarni2024 3 ปีที่แล้ว +67

    Face Cam Videos Are Lit..And This setup is perfect please make all upcoming series with same setup...Its mindblowing...Kudos

  • @sparshsharma3150
    @sparshsharma3150 3 ปีที่แล้ว +51

    This tree series is the most well curated on the entire youtube. Saying this after exploring everything.
    Thank you striver bhaiya ❤️

    • @tech_wizard9315
      @tech_wizard9315 3 ปีที่แล้ว +1

      I am a DSA beginner,is this trees series enough to crack tech giant's like Microsoft linkedin level companies?

    • @sparshsharma3150
      @sparshsharma3150 3 ปีที่แล้ว +8

      @Raj It depends on how good your grasp is on the concepts taught

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

    Yayyyyy... I was so scared of trees but now I started doing it.. This time I found the brute and optimal by myself.. One step at a time☺

  • @kaustubhwaghavkar9079
    @kaustubhwaghavkar9079 2 ปีที่แล้ว +12

    Intuition for predecessor:
    -Basically if root.val >= p.val then just go left because predecessor should ideally be on the left so we do not even have a potential predecessor to set at this point because it has to be smaller than the p's val.
    -On the other hand, if root.val < p.val, then we can have a potential predecessor(may or may not be immediate) so we store root.val into predecessor and move right to find a larger value than current predecessor but less than p's val (basically closer value to p but should be less than p of course).
    -In the end when we reach null, we will have predecessor which we will return.
    Python Code:
    def inorderPredecessor(self, root, p):
    predecessor = None
    while root:
    if root.val >= p.val:
    root = root.left
    else:
    predecessor = root
    root = root.right
    return predecessor
    Complexity Analysis(will be same as successor)
    TC: O(H) because we traverse the height of the tree (left, right, left, right) and skip the other subtree entirely so we are saving time there too.
    SC: O(1) because we are again just changing root pointer all the time and not using recursion to have auxiliary stack space too.

    • @rickk3300
      @rickk3300 ปีที่แล้ว

      Hi Kaustubh, I had one doubt: Can we find the predecessor and the successor together in a single iteration?

    • @srijall
      @srijall ปีที่แล้ว

      ​@@rickk3300 yes we can, maintain two pointers and two answers and update them independently acc to their respective conditions.

    • @rickk3300
      @rickk3300 ปีที่แล้ว

      @@srijall can you send the code?

  • @shantanuahuja5256
    @shantanuahuja5256 2 ปีที่แล้ว +1

    I solved successor many times but didn't come up with all the cases on my own and by watching our video today everything is clear to me .Maza aagaya

  • @tejasmishra7130
    @tejasmishra7130 3 ปีที่แล้ว +62

    Inorder predecessor in BST of given Node
    class Solution {
    public:
    TreeNode * inorderPredecessor(TreeNode * root, TreeNode * p) {
    TreeNode* predecessor = NULL;
    while(root!=NULL){
    if(root->val >= p->val){
    root = root->left;
    }
    else{
    predecessor = root;
    root = root->right;
    }
    }
    return predecessor;
    }
    };

  • @kotipallimadhumeher71
    @kotipallimadhumeher71 2 ปีที่แล้ว +11

    For predecessor,which ever nodes is smaller than val,that is one of possible predecessor
    Then we move to right side to check whether there are any nodes greater than current predecessor and smaller than val
    If node is greater than val,then we'll move to the left.Because we know,On the right side of node which has bigger value also have bigger values so there is no chance there will be a predecessor.
    /*code*/
    class Solution{
    public TreeNode inorderPredecesspr(TreeNode root,TreeNode p){
    TreeNode predecesor=null;
    while(root!=null){
    if(root.val>=p){
    root=root.left;
    }
    else{
    predecesor=root;
    root=root.right;
    }
    }
    return predecesor;
    }
    }

    • @vishalgupta7522
      @vishalgupta7522 3 หลายเดือนก่อน

      thanks bro for the solution

  • @Mr.Indianwxvhehsy9191hghgx
    @Mr.Indianwxvhehsy9191hghgx 9 หลายเดือนก่อน

    while(curr!=NULL){
    if(curr->valval;
    cur=cur->right;
    }
    else{
    cur=cur->left;
    }
    }
    return ans;

  • @stith_pragya
    @stith_pragya 11 หลายเดือนก่อน +1

    UNDERSTOOD.........Thank You So Much for this wonderful video..........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻

  • @rishabhkumar8115
    @rishabhkumar8115 3 ปีที่แล้ว +29

    CODE: for predecessor
    Node* inOrderPredecessor(Node* root, Node* p){
    Node* predecessor = NULL;
    while(root){
    if(root->val right;
    }
    else
    root = root->left;
    }
    return predecessor;
    }

    • @elijahmikaelson740
      @elijahmikaelson740 ปีที่แล้ว

      sir i think there is a problem with this code when i am applying this on the tree with which the striver had explained the optimal solution of successor then it's giving wrong predecessor please check your code once (i tried to find the predecessor of 4).

    • @verma_jay
      @verma_jay หลายเดือนก่อน +1

      You should remove the equal to condition when storing the predecessor, otherwise it will give wrong output

  • @harishsn4866
    @harishsn4866 2 ปีที่แล้ว +4

    You explained it in such a intuitive and simple way. Thank you for this, man.

  • @ravisingh-el8np
    @ravisingh-el8np ปีที่แล้ว

    predecessor code :
    Node * curr = root;
    pre = NULL;
    while(curr!=NULL){
    if(curr->keyright;
    }
    else{
    curr = curr->left;
    }
    }

  • @rohanmalik1772
    @rohanmalik1772 ปีที่แล้ว +3

    For Predecessor if root->val >=target -> Go Left, Else Save this node as successor and then go right. Repeat until current null becomes null

  • @kushagrasahay
    @kushagrasahay 4 หลายเดือนก่อน

    this intuition for finding inorder predecessor and successor is similar to finding floor and ceil of node in BST. simple binary search applied to a tree. Anyways great explanation, striver bhaiya. really inspired.

  • @jayantmishra6897
    @jayantmishra6897 2 ปีที่แล้ว

    he is like a dronaachrya for us and we are eklavya for him. he don't know about us but we are the student of him.

  • @World-Of-Mr-Motivater
    @World-Of-Mr-Motivater 5 หลายเดือนก่อน +1

    to find the inorder predecessor
    we can do the following:
    i) go to node left and store it in predecessor variable
    ii)then go to it's right and store it
    so inshort:
    move to the node left
    if node has right move to it
    if node is empty then the one at the rightmost is our answer;

  • @anshulsharma3137
    @anshulsharma3137 3 ปีที่แล้ว +11

    Isn't its just the Ceil of BST ? Btw the whole series is too good

  • @gabrieldias6430
    @gabrieldias6430 5 หลายเดือนก่อน

    perfect explanation! I guess the only thing that doesn't make this video better is that you implemented iteractively, but, except from that, really good video! Thanks for sharing your knownledge!

  • @isheep9025
    @isheep9025 ปีที่แล้ว

    self note alternate appraoch with 0(H+H) TC and 0(1) sc- find the note using binary seach tree algo, keeping track of previous node visited,if we find a leaf node return last node which we kept a track of ,else find leftmost node of the right subtree from the node we found out,if we have encountered the last node return null

  • @thinkingmad1685
    @thinkingmad1685 2 ปีที่แล้ว +2

    So my answer of predecessor will be similar to finding floor value approach
    TreeNode * predecessor(TreeNode* root, TreeNode * p) {
    TreeNode * pred =nullptr;
    If(!root) return pred;
    While(root! =nullptr) {
    If(root->valval) {
    pred=root;
    root=root->right;
    }
    else
    root=root->left;
    }
    }
    return pred;
    }

  • @amanshah1650
    @amanshah1650 3 หลายเดือนก่อน

    Another approach :- O(H) time complexity and O(1) space complexity
    To find suc and pre for key.(covered both the cases key in tree/not in tree)
    void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
    {
    pre=NULL;suc=NULL;
    while(root){
    if(root->key==key){
    if(root->left){
    auto rightmost=root->left;
    while(rightmost->right)rightmost=rightmost->right;
    pre=rightmost;
    }
    if(root->right){
    auto leftmost=root->right;
    while(leftmost->left)leftmost=leftmost->left;
    suc=leftmost;
    }
    break;
    }
    else if(root->key>key){
    suc=root;
    root=root->left;
    }
    else {
    pre=root;
    root=root->right;
    }
    }
    }

  • @misterr_stupied3747
    @misterr_stupied3747 ปีที่แล้ว

    to find immediate smaller value of node we can store node.val which is greater then succsesor and smaller then the given input

  • @abhaypratapsingh1457
    @abhaypratapsingh1457 ปีที่แล้ว +2

    if(p->val>root->val){
    predecessor = root;
    root = root->right;
    }
    else{
    root = root->left;
    }

  • @sujan_kumar_mitra
    @sujan_kumar_mitra 3 ปีที่แล้ว +9

    In case of predecessor,
    When we go right, we set current node as possible ans

  • @bhumithakur9967
    @bhumithakur9967 2 ปีที่แล้ว +1

    In case of predecessor , we'll store the value less than given val for that..so when we move right we store root in curr val..

  • @Avinashkumar-yq2zr
    @Avinashkumar-yq2zr 2 ปีที่แล้ว

    TreeNode * inorderPredecessor(TreeNode * root, TreeNode * p)
    {
    TreeNode*ans=NULL;
    while(root)
    {
    if(root->val < p->val)
    {
    ans=root;
    root=root->right;
    }
    else
    root=root->left;
    }
    return ans;
    }

  • @akashpal7748
    @akashpal7748 ปีที่แล้ว +2

    Hey striver bhaiya, here is the homework
    public static Node findPre(Node root,int key,int node){
    if(root == null){
    return new Node(node);
    }
    if(key>root.data){
    return findPre(root.right,key,root.data);
    }
    return findPre(root.left,key,node);
    }

  • @AnimishTripathy
    @AnimishTripathy ปีที่แล้ว +3

    HOMEWORK DONE STRIVER BHAIYA 👍
    Node* findPredecessor(Node* root, Node* p) {
    if(!root)
    {
    return NULL;
    }
    Node* predecessor=NULL;
    Node* curr=root;
    while(curr)
    {
    if(curr->data < p->data)
    {
    predecessor=curr;
    curr=curr->right;
    }
    else
    {
    curr=curr->left;
    }
    }
    return predecessor;
    }

  • @apmotivationakashparmar722
    @apmotivationakashparmar722 9 วันที่ผ่านมา

    Thank you so much Striver !

  • @rishabhbatra3022
    @rishabhbatra3022 3 ปีที่แล้ว +12

    🔴Code for Predecessor, Just opposite of successor, so just change the if-else conditions according to question demand and It will work fine🔴
    TreeNode* inorderPredecessor(TreeNode* root, TreeNode* p) {
    TreeNode* predecessor = NULL;

    while (root != NULL) {

    if (p->val < root->val) {
    root = root->left;
    } else {
    predecessor = root;
    root = root->right;
    }
    }

    return predecessor;
    }

    • @takeUforward
      @takeUforward  3 ปีที่แล้ว +6

      Yeah

    • @shineinusa
      @shineinusa 3 ปีที่แล้ว +15

      😌This code fails to find the predecessor, you are travelling left only when the target value is less than root value, but you are not travelling left when both target and root values are equal!
      Correction would be
      TreeNode predecessor = null;
      while (root != null) {
      if (p.val

    • @neilchaudhary007
      @neilchaudhary007 3 ปีที่แล้ว +5

      @@takeUforward the above solution will not work, try DRY of this solution the solution that will work is this one
      TreeNode* successor(TreeNode* root, TreeNode* k){
      TreeNode* pre = NULL;
      while(root != NULL){
      if(k -> val > root -> val ){
      pre = root;
      root = root -> right;
      }
      else
      root = root -> left;
      }
      return pre;
      }
      and the error in above solution is instead of

    • @adityaajay29
      @adityaajay29 2 ปีที่แล้ว

      @@shineinusa yeah

  • @nikhilagrawal9217
    @nikhilagrawal9217 2 หลายเดือนก่อน

    Thanks for the 3rd approach.

  • @ravipatiramya4185
    @ravipatiramya4185 3 ปีที่แล้ว

    If(p->Val>=root->Val) predecessor=root ;root=root->right;
    Else root=root->left;
    Return predecessor

    • @neilchaudhary007
      @neilchaudhary007 3 ปีที่แล้ว

      This solution will not work because in this what will happen when we check p-> val >= root->val it will update the predecessor to the given root and not the value just before if so that you need to right the same solution and now instead of >= use only > and you will be good to go otherwise you can DRY your code and then you will see and here is the updated code
      TreeNode* successor(TreeNode* root, TreeNode* k){
      TreeNode* pre = NULL;
      while(root != NULL){
      if(k -> val > root -> val ){
      pre = root;
      root = root -> right;
      }
      else
      root = root -> left;
      }
      return pre;
      }

  • @InfinteMotivation
    @InfinteMotivation ปีที่แล้ว

    mind blowing striver yr _/\_ huge respect

  • @pururajsinghrajawat5348
    @pururajsinghrajawat5348 2 ปีที่แล้ว +1

    It's the same concept as ceil and floor of a BST with a little difference that ceil and floor can't be equal to the given key.

  • @PriyanshJwalantYadav
    @PriyanshJwalantYadav ปีที่แล้ว

    Man that's mind blowing.....everything cleared!!!
    Thanks a lot Brother

  • @sidhantjha4566
    @sidhantjha4566 2 ปีที่แล้ว

    Predecessor
    while(root){
    if(keykey){
    root = root->left;
    }
    else{
    pre = root;
    root = root->right;
    }
    }

  • @iriyaagrawal
    @iriyaagrawal ปีที่แล้ว +1

    code for predecessor will be
    while(root!=null){
    if(key>root.data){
    pre=root;
    root=root.right;
    }
    else{
    root=root.left;
    }
    }

  • @Mysterious_debris_1111
    @Mysterious_debris_1111 3 ปีที่แล้ว +1

    Great content... Please do maintain this level

  • @aswinikumarsahoo6459
    @aswinikumarsahoo6459 2 ปีที่แล้ว

    Inorder predecessor of BST in Java (node to be returned) :
    Node inorderPredecessor(Node root, Node target)
    {
    Node predecessor=null;
    while(root!=null)
    {
    if(target.data > root.data)
    {
    predecessor=root;
    root=root.right;
    }
    else if(target.data

  • @eklavyaprasad5009
    @eklavyaprasad5009 3 ปีที่แล้ว +1

    predecessor=null;
    if(p.val

  • @deepaktiwari7059
    @deepaktiwari7059 2 ปีที่แล้ว

    Python code to find both predecessor & Successor :
    def findPreSuc(root, pre, suc, key):
    temp = root
    while root and root.key != key:
    if root.key < key:
    pre[0] = root
    root = root.right
    else:
    suc[0] = root
    root = root.left

    # Predecessor is going to be right most node on the left subtree if left node exist
    if root.left:
    prev = root.left
    while prev.right:
    prev = prev.right
    pre[0] = prev
    # successor is going to be left most node on the right subtree if right node exist
    if root.right:
    prev = root.right
    while prev.left:
    prev = prev.left
    suc[0] = prev

  • @rishabhgupta9846
    @rishabhgupta9846 ปีที่แล้ว +2

    Code for both Successor/Predecessor
    void findPreSuc(Node *root, Node *&pre, Node *&suc, int key)
    {
    pre = NULL;
    suc = NULL;
    Node *node = root;
    while (node)
    {
    if (node->key right;
    }
    else
    {
    suc = node;
    node = node->left;
    }
    }
    node = root;
    while (node)
    {
    if (node->key >= key)
    {
    node = node->left;
    }
    else
    {
    pre = node;
    node = node->right;
    }
    }
    }

  • @shivanjali3909
    @shivanjali3909 6 หลายเดือนก่อน

    Method 2 implemented by Recursively
    void presuc(TreeNode* root, int& pre, int& suc, int key)
    {
    if (root == NULL)
    return;
    presuc(root->left, pre, suc, key);
    if (root->data < key)
    pre = root->data;
    if (root->data > key && suc == -1)
    suc = root->data;
    presuc(root->right, pre, suc, key);
    }

  • @jarangvinayak.5435
    @jarangvinayak.5435 2 หลายเดือนก่อน

    TreeNode predecessor=null;
    TreeNode predecessor(TreeNode root,TreeNode val){
    if(root==null){
    return null;
    }
    if(root.val

  • @aaryanverma2000
    @aaryanverma2000 2 ปีที่แล้ว

    Thanku bhaiya. You're the best

  • @anirbanbera5630
    @anirbanbera5630 2 ปีที่แล้ว

    Predecessor
    Node*pre=NULL;
    while(root){
    if(root->datadata){
    pre=root;
    root=root->right;
    }
    else{
    root=root->left;
    }
    }

  • @charuprabha9696
    @charuprabha9696 2 หลายเดือนก่อน

    first we will traverse and reach the node
    for predecessor we will find the max value from the left subtree
    for succesor we will find the minimum value from the right subtree

  • @avinashbhatia3303
    @avinashbhatia3303 2 ปีที่แล้ว +1

    Inorder successor is same as finding ceil in a string.

  • @MadhurGupta-f7p
    @MadhurGupta-f7p 3 หลายเดือนก่อน

    bro you are the best

  • @rakshitpandey7517
    @rakshitpandey7517 2 ปีที่แล้ว

    Simplest Solution- (Before seeing the Soln in the video)...:)
    Node * inOrderSuccessor(Node *root, Node *x)
    {
    Node *curr=NULL;
    while(root)
    {
    if(root->data>x->data)
    {
    curr=root;
    root=root->left;
    }
    else
    {
    root=root->right;
    }
    }
    return curr;
    }

  • @neelshah6695
    @neelshah6695 ปีที่แล้ว

    🌟Predecessor Code 🌟
    while(root != null){
    if(root.val >= x.val)
    root = root.left;
    else{
    predecessor = root;
    root = root.right;
    }
    }
    return predecessor;

  • @nikhilkumar-jy6mz
    @nikhilkumar-jy6mz หลายเดือนก่อน

    wow this is same as finding ceil or floor of bst problem,

  • @ettireddyspandana
    @ettireddyspandana ปีที่แล้ว

    Node* Pred(Node* root, Node* p)
    {
    Node* predi=NULL;
    while(root)
    {
    if(root->val >= p->val) root=root->left;
    else
    {
    predi=root;
    root=root->right;
    }
    return predi;
    }

  • @mugambo5505
    @mugambo5505 2 ปีที่แล้ว

    predecessor
    int sucess(node *root, int key)
    {
    static int ans = INT_MIN;
    if (root == NULL)
    {
    return -1;
    }
    if (root->data >= key)
    {
    sucess(root->left, key);
    }
    else
    {
    ans = max(ans, root->data);
    sucess(root->right, key);
    }
    return ans;
    }

  • @rosonerri-faithful
    @rosonerri-faithful 2 ปีที่แล้ว +1

    @Striver bro, successor=ceil and predecesor =floor. Isn't it?

  • @akhilkishore7361
    @akhilkishore7361 3 ปีที่แล้ว +1

    make a video on switching service to product based

  • @ideepakpandey
    @ideepakpandey 2 ปีที่แล้ว

    predcessor code:-
    TreeNode* predcessor(TreeNode* root,TreeNode* p){
    TreeNode* pred=NULL;
    while (root!=NULL)
    {
    if(p->data > root->data){
    pred=root;
    root=root->right;
    }
    else{
    root=root->left;
    }
    }
    return pred;
    }

  • @shreyxnsh.14
    @shreyxnsh.14 20 ชั่วโมงที่ผ่านมา

    Code for both successor and predecessor:
    class Solution
    {
    public:
    Node* findPre(Node* root, int key){
    Node* ans = NULL;
    while(root){
    if(root->key < key){
    ans = root;
    root = root->right;
    }else{
    root = root->left;
    }
    }
    return ans;
    }
    Node* findSuc(Node* root, int key){
    Node* ans = NULL;
    while(root){
    if(root->key > key){
    ans = root;
    root = root->left;
    }else
    root = root->right;
    }
    return ans;
    }
    void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
    {
    // Your code goes here
    pre = findPre(root, key);
    suc = findSuc(root, key);
    }
    };

  • @atharvacharde5628
    @atharvacharde5628 2 ปีที่แล้ว

    Sir these approach is no doubt good but consider a case in which only key given for which successor has to be find and not root given then how to find out it?

  • @amitsinghrana6619
    @amitsinghrana6619 2 ปีที่แล้ว +1

    Isn't this same as Upper-bound/Lower-bound in BST.✅

  • @amanbhadani8840
    @amanbhadani8840 2 ปีที่แล้ว +1

    CODE FOR INORDER PREDECESSOR ----
    class Solution {
    public:
    TreeNode* inorderPredecessor(TreeNode* root, TreeNode* p) {
    TreeNode* Predeccessor = NULL;

    while (root != NULL) {

    if (p->val > root->val) {
    Predeccessor = root;
    root = root->right;
    } else {
    root = root->left;
    }
    }

    return Predeccessor;
    }
    };

  • @adityagarg9806
    @adityagarg9806 2 ปีที่แล้ว

    It's basically equal to finding ceil

  • @vaalarivan_p
    @vaalarivan_p ปีที่แล้ว +1

    1:50 - 3:40

  • @shadabfaisal801
    @shadabfaisal801 2 ปีที่แล้ว

    public static TreeNode inorderPredecessor(TreeNode root, TreeNode p) {
    TreeNode predecessor=null;

    while(root!=null) {
    if(p.val>root.val) {
    predecessor=root;
    root=root.right;
    }
    else {
    root=root.left;
    }
    }
    return predecessor;
    }

  • @shinewanna3959
    @shinewanna3959 ปีที่แล้ว

    Presuccessor Answer in Dart
    presuccessor(TreeNode? root, int target) {
    int? presucessor;
    f(TreeNode? root) {
    if (root == null) {
    return null;
    }
    if (root.val < target) {
    presucessor = root.val;
    f(root.right);
    } else if (root.val >= target) {
    f(root.left);
    }
    }
    f(root);
    return presucessor;
    }

  • @horccruxxxop4184
    @horccruxxxop4184 3 ปีที่แล้ว

    Pls aese he tutorials lekr aeye

  • @lavanyaprakashjampana933
    @lavanyaprakashjampana933 ปีที่แล้ว

    we love your content and we love you....🖤

  • @akashsahu2571
    @akashsahu2571 ปีที่แล้ว +1

    yes

  • @shubamgoswami
    @shubamgoswami 2 ปีที่แล้ว +1

    it is basically ceil in bst

  • @gunjanbiswas925
    @gunjanbiswas925 3 ปีที่แล้ว +3

    predecessor ke liye immediate chota wala element chaiye...so we will move accordingly

  • @Learnprogramming-q7f
    @Learnprogramming-q7f 7 หลายเดือนก่อน

    Thank you Bhaiya

  • @ruchitasharma2842
    @ruchitasharma2842 2 ปีที่แล้ว

    For Both the Predecessor and Successor:
    ArrayList list = new ArrayList();
    int successor = -1;
    int predecessor =-1;
    while(root!=null){
    if(key>root.val){
    predecessor = root.val;
    root= root.right;
    }
    if(key< root.val){
    successor = root.val;
    root= root.left;
    }
    }
    list.add(predecessor);
    list.add(successor);
    return list;
    }
    Is it correct?

    • @devanshakruvala1354
      @devanshakruvala1354 2 ปีที่แล้ว

      Nope!! when key==root, root will not change and it will result in TLE.

    • @shivansh709
      @shivansh709 ปีที่แล้ว

      @@devanshakruvala1354 thank you! ive been thinking about this for hours where i was wrong lol

  • @Aditya-mx8rp
    @Aditya-mx8rp ปีที่แล้ว

    thank you

  • @debugagrawal
    @debugagrawal 2 ปีที่แล้ว

    It's ame Question as the FLOOR and CIEL value 🚀✅📄

  • @vikrambabariya5166
    @vikrambabariya5166 2 ปีที่แล้ว

    You are doing very nice work, keep it up !

  • @vaibhavsharma4553
    @vaibhavsharma4553 3 หลายเดือนก่อน

    Node* inorderpredesessor(Node* root, int val){
    Node* pred = root;
    while(root!=NULL){
    if(val >= root->data){
    pred = root;
    root = root->right;
    }
    else{

    root = root->left;
    }
    }
    return pred;
    }

  • @ashugupta6248
    @ashugupta6248 2 ปีที่แล้ว +4

    ye pura floor or ceil Binary Search tree jesa nahi lag rha ha???

  • @mrrishiraj88
    @mrrishiraj88 3 ปีที่แล้ว +1

    Thanks

  • @mandeepsinghsoorma3080
    @mandeepsinghsoorma3080 2 ปีที่แล้ว +1

    This is the solution to the codeStudio problem "Finding the predecessor and successor in a BST"
    pair predecessorSuccessor(BinaryTreeNode* root, int key) {
    auto p=-1, s=-1;

    // finding predecessor
    auto t=root;
    while(t) {
    if(t->data < key) p=t->data, t=t->right;
    else t=t->left;
    }

    // finding successor
    t=root;
    while(t) {
    if(t->data > key) s=t->data, t=t->left;
    else t=t->right;
    }
    return {p,s};
    }

  • @ankushmallick6968
    @ankushmallick6968 หลายเดือนก่อน +1

    Solution for Inorder Successor and Predecessor:
    class Solution
    {
    public:
    void findPreSuc(Node* root, Node*& pre, Node*& suc, int key)
    {
    // Your code goes here
    Node* curr_pre = root;
    Node* curr_suc = root;
    while(curr_pre){
    if(key key){
    curr_pre = curr_pre->left;
    } else{
    pre = curr_pre;
    curr_pre = curr_pre->right;
    }
    }
    while(curr_suc){
    if(key >= curr_suc->key){
    curr_suc = curr_suc->right;
    } else{
    suc = curr_suc;
    curr_suc = curr_suc->left;
    }
    }
    }
    };

  • @theautumnleaves343
    @theautumnleaves343 7 หลายเดือนก่อน

    void predecessor(Node* root,Node*& pre,int key) {
    while(root) {
    if(root->keyright;
    }else root=root->left;
    }
    }
    void successor(Node* root, Node*& suc, int key) {
    while(root) {
    if(key>=root->key) root=root->right;
    else {
    suc=root;
    root=root->left;
    }
    }
    }

  • @ghj7275
    @ghj7275 2 ปีที่แล้ว

    class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
    TreeNode predecessor = null;
    while(root != null) {
    if(root.val > p.val) {
    root = root.left;
    } else if(root.val == p.val) {
    root = root.left;
    } else {
    if(predecessor == null || predecessor.val > root.val)
    predecessor = root;
    root = root.right;
    }
    }
    return predecessor;
    }
    }
    @takeUforward code for finding predecessor

  • @rashmi_gaur
    @rashmi_gaur 2 ปีที่แล้ว

    Thank you Brother!

  • @UECAshutoshKumar
    @UECAshutoshKumar ปีที่แล้ว +1

    Thank you sir

  • @codetochange8
    @codetochange8 2 ปีที่แล้ว

    Loved your explanation bro!!!

  • @sawankundu1898
    @sawankundu1898 ปีที่แล้ว

    //Predecessor
    public static void predecessor(Node root, Res p, int key){
    while(root != null){
    if(root.data >= key){
    root = root.left;
    }else{
    p.pre = root;
    root = root.right;
    }
    }
    }

  • @Codebreaker0702
    @Codebreaker0702 ปีที่แล้ว

  • @sanketatmaram
    @sanketatmaram 2 หลายเดือนก่อน

    understood!

  • @average_aspirant
    @average_aspirant 2 ปีที่แล้ว

    what is the difference between inorder successor and ceil of BST.

  • @sarthaksharma6973
    @sarthaksharma6973 2 ปีที่แล้ว

    So it is basically the ceil and floor of a number in a bst

  • @Shivi32590
    @Shivi32590 2 หลายเดือนก่อน

    understood

  • @anishamajumder1940
    @anishamajumder1940 5 หลายเดือนก่อน

    Node* inorderPre(Node* root,Node* p){
    Node*predecessor=NULL;
    while(root){
    if(p->datadata){
    root=root->left;
    }else{
    predecessor=root;
    root=root->rigth;
    }
    }
    return predecessor;
    }

  • @divy04
    @divy04 5 หลายเดือนก่อน

    Predecessor code
    Node* temp=root;
    Node* par=NULL;
    int k=x->data;
    while(temp){
    if(temp->dataright;}
    else if(temp->data>k){temp=temp->left;}
    else temp=temp->left;
    }
    if(par==NULL||par->data>=k)return NULL;
    return par;

  • @nishanth1828
    @nishanth1828 2 ปีที่แล้ว

    Isn't it a modified Ceil/ Floor problem?

  • @satyapraneethkatta1652
    @satyapraneethkatta1652 2 ปีที่แล้ว

    Thanks a lot again Striver

  • @harshitjaiswal9439
    @harshitjaiswal9439 8 หลายเดือนก่อน

    understood.

  • @deepaktuli1
    @deepaktuli1 2 ปีที่แล้ว

    public static BinaryTreeNode inOrderPredecessor(BinaryTreeNode root,
    BinaryTreeNode p){
    BinaryTreeNode predecessor = null;
    while(root!=null) {
    if(root.data >= p.data) root = root.left;
    else {
    predecessor = root;
    root = root.right;
    }
    }
    return predecessor;
    }

  • @ankitchoudhary2017
    @ankitchoudhary2017 2 ปีที่แล้ว

    TreeNode *predecessor=NULL;
    while(root!= NULL)
    {
    if(root->val val)
    {root=root->right;
    predecessor=root; }
    else
    { root=root->left;}
    }
    return predecessor;