''' 題目描述 給定一個二叉樹和其中的一個結點,請找出中序遍歷順序的下一個結點並且返回。 注意,樹中的結點不僅包含左右子結點,同時包含指向父結點的指針。 ''' # -*- 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 is not None: root=pNode.right while(root.left is not None): root=root.left return root else: # 如果當前節點的父節點的左孩子是當前節點,則直接返回當前節點的父節點 if pNode.next is None: return None if pNode.next.left==pNode: return pNode.next while(pNode.next and pNode.next.right==pNode): pNode=pNode.next return pNode.next
python 二叉樹的下一個結點
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.