寻找二叉树的后继节点

后继节点为中序遍历中一个节点的下一个节点

class Node(object):
    def __init__(self,val=Node):
        self.val = val
        self.left = None
        self.right = None
        self.parent = None
def getSuccessorNode(node):
    if node == None:
        return node
    if node.right != None:
        return getLeftMost(node.right)
    else:
        parent = node.parent
        while parent!=None and parent.left!=node:
            node = parent
            parent = node.parent
        return parent
def getLeftMost(node):
    while node != None:
        node =node.left
    return node

 

你可能感兴趣的:(算法牛人之路,寻找二叉树的后继节点)