-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
二叉搜索树的第k个结点.py
52 lines (50 loc) · 1.36 KB
/
二叉搜索树的第k个结点.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
'''
给定一颗二叉搜索树,请找出其中的第k大的结点。例如,
5
/ \
3 7
/\ /\
2 4 6 8 中,
按结点数值大小顺序第三个结点的值为4。
'''
# -*- coding:utf-8 -*-
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
# 返回对应节点TreeNode
def __init__(self):
self.treeNode = []
def inOrder(self, pRoot):
if len(self.treeNode) < 0:
return None
if pRoot.left:
self.inOrder(pRoot.left)
self.treeNode.append(pRoot)
if pRoot.right:
self.inOrder(pRoot.right)
def KthNode(self, pRoot, k):
if k == 0 or pRoot == None:
return
self.inOrder(pRoot)
if len(self.treeNode) < k:
return None
return self.treeNode[k-1]
def KthNode2(self, pRoot, k):
if k <= 0 or not pRoot:
return None
treeStack, nodesQue = [], []
pNode = pRoot
while pNode or len(treeStack):
while pNode:
treeStack.append(pNode)
pNode = pNode.left
if len(treeStack):
pNode = treeStack.pop()
nodesQue.append(pNode)
pNode = pNode.right
if k > len(nodesQue):
return None
return nodesQue[k-1]