劍指offer Python版 - 二叉樹的下一個結點
阿新 • • 發佈:2018-11-20
題目描述
給定一個二叉樹和其中的一個結點,請找出中序遍歷順序的下一個結點並且返回。注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指標。# -*- 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 not pNode:
return pNode
if pNode.right:
current = pNode.right
if current.left:
current = current.left
return current
while pNode.next:
parent = pNode.next
if parent.left == pNode:
return parent
pNode = parent
執行時間:44ms
佔用記憶體:5732k
注:執行結果資料來源牛客網。