剑指offer 二叉树的下一个结点 Posted on 2019-02-21 | 二叉树的下一个结点类似于Leetcode 173. 二叉搜索树迭代器 题目给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。 方法123456789101112131415161718192021222324252627282930313233343536373839404142434445464748# -*- coding:utf-8 -*-# class TreeLinkNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = None# self.next = Noneclass Solution: def GetNext(self, pNode): # write code here if not pNode: return if pNode.right: node=pNode.right while node.left: node=node.left return node else: while pNode.next: if pNode==pNode.next.left: return pNode.next pNode=pNode.next return None``` ```python# -*- coding:utf-8 -*-# class TreeLinkNode:# def __init__(self, x):# self.val = x# self.left = None# self.right = None# self.next = Noneclass Solution: def GetNext(self, pNode): # write code here if not pNode: return if pNode.right: node=pNode.right while node.left: node=node.left return node else: while pNode.next and pNode==pNode.next.right: pNode=pNode.next return pNode.next return None