Skip to content

Instantly share code, notes, and snippets.

@ryancooper
Created July 8, 2014 18:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryancooper/a87fb60f8f9b7865ecf1 to your computer and use it in GitHub Desktop.
Save ryancooper/a87fb60f8f9b7865ecf1 to your computer and use it in GitHub Desktop.
# 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