-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path二叉树的下一个结点.py
34 lines (32 loc) · 1.1 KB
/
二叉树的下一个结点.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'''
题目描述
给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。
'''
# -*- 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 is None:
return
pNext = None
if pNode.right:
pNode = pNode.right
while pNode.left:
pNode = pNode.left
pNext = pNode
else:
if pNode.next and pNode.next.left == pNode:#.next代表指向root节点
pNext = pNode.next
elif pNode.next and pNode.next.right == pNode:
pNode = pNode.next
while pNode.next and pNode.next.right == pNode:
pNode = pNode.next
if pNode.next:
pNext = pNode.next
return pNext