Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- vvv imp
- while finding inorder predecessor if you are at target node then still you have to continue to left to check whether any element exists whose value is less than target and update it with predecessor
- '''
- class Solution:
- """
- @param root: the given BST
- @param p: the given node
- @return: the in-order predecessor of the given node in the BST
- """
- def inorderPredecessor(self, root, p):
- cur=root
- pre=None
- while cur:
- if p.val<=cur.val:
- cur=cur.left
- else:
- pre=cur
- cur=cur.right
- return pre
- '''
- while finding inorder successor if you are at target node then still you have to continue to left to check whether any element exists whose value is less than target and update it with successor
- '''
- class Solution:
- """
- @param: root: The root of the BST.
- @param: p: You need find the successor node of p.
- @return: Successor of p.
- """
- def inorderSuccessor(self, root, p):
- succ=None
- cur=root
- while cur:
- if p.val<cur.val:
- succ=cur
- cur=cur.left
- else:
- cur=cur.right
- return succ
Advertisement
Add Comment
Please, Sign In to add comment