题解 | #二叉树的下一个结点#

二叉树的下一个结点

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

全部评论

相关推荐

点赞 评论 收藏
分享
06-25 09:33
厦门大学 Java
球球别拷打俺了:现在日常估计没啥hc了,等到八月多估计就慢慢有了。双九✌🏻不用焦虑的
投递快手等公司10个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务