Fetch the repository succeeded.
This action will force synchronization from 陌溪/LearningNotes, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
https://www.nowcoder.com/practice/9023a0c988684a53960365b889ceaf5e
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
class TreeLinkNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
self.next = None
class Solution:
def GetNext(self, pNode):
# 1.寻找右子树,如果存在就一直找到右子树的最左边,就是下一个节点
# 2.没有右子树,就寻找他的父节点,一直找到它是父节点的左子树,打印父节点
if pNode.right:
tmpNode = pNode.right
while tmpNode.left:
tmpNode = tmpNode.left
return tmpNode
else:
tmpNode = pNode
while tmpNode.next:
if tmpNode.next.left == tmpNode:
return tmpNode.next
tmpNode = tmpNode.next
return None
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。