mhr007

Inorder Successor in BST

Oct 22nd, 2021
591
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Definition for a binary tree node.
  2. # class TreeNode:
  3. #     def __init__(self, x):
  4. #         self.val = x
  5. #         self.left = None
  6. #         self.right = None
  7.  
  8. class Solution:
  9.    
  10.     def inorder(self,node,found,p,arr):
  11.        
  12.         if node == None:
  13.             return None
  14.        
  15.            
  16.         if node.left != None:
  17.             found = self.inorder(node.left,found,p,arr)
  18.        
  19.        
  20.         if found == False:
  21.             arr.append(node.val)
  22.        
  23.         if node.val == p:
  24.             found = True
  25.        
  26.        
  27.            
  28.         if node.right  != None:
  29.             found = self.inorder(node.right,found,p,arr)
  30.        
  31.         return found
  32.    
  33.     def inorderSuccessor(self, root: 'TreeNode', p: 'TreeNode') -> 'TreeNode':
  34.         arr =[]
  35.         t = self.inorder(root,False,p,arr)
  36.         print(arr[-1])
  37.         print(t)
  38.        
  39.        
RAW Paste Data