Created
July 8, 2014 18:02
-
-
Save ryancooper/a87fb60f8f9b7865ecf1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node | |
class TreeNode: | |
def __init__(self, x): | |
self.val = x | |
self.left = None | |
self.right = None | |
self.parent = None | |
def successor(node): | |
# return the tree node which is the successor of this node | |
if node.right: | |
node = node.right | |
while node.left: | |
node = node.left | |
return node | |
else: | |
while node.parent and node.parent.left != node: | |
node = node.parent | |
return node.parent |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment