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

二叉树的下一个结点

http://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):
        # write code here
        #两种情况,如果右子树不为空,那么就是右子树里最左边的结点
        #如果右子树为空,就往上找,找到一个左孩子是当前结点的祖先结点
        if pNode.right:
            tmp=pNode.right
            while tmp.left:
                tmp=tmp.left
            return tmp
        else:
            while pNode.next:
                parent=pNode.next
                if parent.left==pNode:
                    return parent
                pNode=pNode.next
        return None
全部评论

相关推荐

02-24 17:39
门头沟学院 Java
神哥不得了:神哥来啦~专业技能的话建议不要前面空那么多,八股的话建议先把高频top 50的八股多巩固几遍,千万不要看那些假高频八股。项目的话,建议换两个高质量的项目上去
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务