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

二叉树的下一个结点

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:
    # 要先找到根节点 next指向父结点->找根结点的
    def __init__(self) -> None:
        self.list = []

    def inorder(self, pNode):
        if not pNode: return
        self.inorder(pNode.left)
        print(pNode.val)
        self.list.append(pNode)
        self.inorder(pNode.right)
        return

    def GetNext(self, pNode):
        # write code here
        root = pNode
        while root.next:
            root = root.next
        self.inorder(root)
        print(self.list)
        flag = False
        for i in self.list:
            if i == pNode:
                flag = True
                continue
            if flag == True:
                return i
        return None

全部评论

相关推荐

头像
04-17 09:29
已编辑
湖南农业大学 后端
睡姿决定发型丫:本硕末9也是0offer,简历挂了挺多,只有淘天 美团 中兴给了面试机会,淘天二面挂,美团一面kpi面,中兴一面感觉也大概率kpi(虽然国企,但一面0技术纯聊天有点离谱吧)
点赞 评论 收藏
分享
VirtualBool:都去逗他了?
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务