题解 | #二叉树的下一个结点#
二叉树的下一个结点
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
# -*- coding:utf-8 -*- # class TreeLinkNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # self.next = None class Solution: def GetNext(self, pNode): # write code here # 如果有右子树,则后继为右子树最左侧的点 if pNode.right: pNode = pNode.right while pNode.left: pNode = pNode.left return pNode # 没有右子树,后继往上找,找到的第一个节点i,满足i是i的father的左孩子 # i的father就是pNode的后一节点 while pNode.next and pNode.next.left != pNode: pNode = pNode.next return pNode.next