Advertisement
nikunjsoni

510

May 8th, 2021
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.60 KB | None | 0 0
  1. /*
  2. // Definition for a Node.
  3. class Node {
  4. public:
  5.     int val;
  6.     Node* left;
  7.     Node* right;
  8.     Node* parent;
  9. };
  10. */
  11.  
  12. class Solution {
  13. public:
  14.     Node* inorderSuccessor(Node* node) {
  15.         // Find smallest node in right subtree.
  16.         if(node->right){
  17.             node = node->right;
  18.             while(node->left)
  19.                 node = node->left;
  20.             return node;
  21.         }
  22.         // else find that parent for which the current node is not right child.
  23.         while(node->parent && node == node->parent->right) node = node->parent;
  24.         return node->parent;
  25.     }
  26. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement