Advertisement
nikunjsoni

285

May 8th, 2021
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.58 KB | None | 0 0
  1. /**
  2.  * Definition for a binary tree node.
  3.  * struct TreeNode {
  4.  *     int val;
  5.  *     TreeNode *left;
  6.  *     TreeNode *right;
  7.  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  8.  * };
  9.  */
  10. class Solution {
  11. public:
  12.     TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
  13.         TreeNode *successor = NULL;
  14.         while(root){
  15.             if(root->val <= p->val){
  16.                 root = root->right;
  17.             }
  18.             else{
  19.                 successor = root;
  20.                 root = root->left;
  21.             }
  22.         }
  23.         return successor;
  24.     }
  25. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement