当前位置: 代码迷 >> 综合 >> 剑指offer7:二叉树的下一个节点
  详细解决方案

剑指offer7:二叉树的下一个节点

热度:27   发布时间:2024-01-25 14:45:58.0
# -*- 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#如果没有结点,返回Noneif not pNode :return None#如果有右子树,下一个节点即右子树的最左子节点if pNode.right:tmp=pNode.rightwhile tmp.left:tmp=tmp.leftreturn tmpelse:#如果没有右子树,当它为左孩子是,下一个节点是父节点,当它为右孩子时,下一个节点为为父节点的父节点while pNode.next:if  pNode.next.left==nNode:return nNode.nextpNode=pNode.nextreturn None

 

  相关解决方案