python剑指offer系列二叉搜索树的第k个结点
2018-03-17 10:08
197 查看
题目:
给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 24 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 KthNode(self, pRoot, k):
self.arr = []
self.midTravel(pRoot)
return self.arr[k-1] if 0 < k <= len(self.arr) else None
def midTravel(self,pRoot):
if pRoot == None:
return
self.midTravel(pRoot.left)
self.arr.append(pRoot)
self.midTravel(pRoot.right)
# write code here
给定一颗二叉搜索树,请找出其中的第k大的结点。例如, 5 / \ 3 7 /\ /\ 24 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 KthNode(self, pRoot, k):
self.arr = []
self.midTravel(pRoot)
return self.arr[k-1] if 0 < k <= len(self.arr) else None
def midTravel(self,pRoot):
if pRoot == None:
return
self.midTravel(pRoot.left)
self.arr.append(pRoot)
self.midTravel(pRoot.right)
# write code here
相关文章推荐
- 《剑指Offer》之“二叉搜索树的第k个结点”
- 剑指Offer系列-面试题15:链表中倒数第K个结点
- 剑指offer—二叉搜索树的第k个结点
- 【剑指offer-解题系列(63)】二叉搜索树的第k个结点
- 剑指offer系列源码-链表中倒数第k个结点
- 剑指offer 面试题63:二叉搜索树的第k个结点(二叉树中序遍历的应用) 题解
- 剑指offer:二叉搜索树的第k个结点
- 剑指offer——面试题63:二叉搜索树的第k个结点
- 剑指offer系列-T15链表中倒数第k个结点
- python剑指offer系列二叉搜索树与双向链表
- 剑指offer系列-T16链表中倒数第k个结点
- 剑指offer(59)-二叉搜索树的第k个结点
- 剑指Offer_62_二叉搜索树的第k个结点
- 剑指Offer——二叉搜索树的第k个结点
- 剑指offer——二叉搜索树的第k个结点(一般)
- 《剑指offer》:[63]二叉搜索树的第K个结点
- 剑指offer——二叉搜索树的第k个结点(覆盖TreeSet的compare方法)
- 剑指offer--面试题63:二叉搜索树的第k个结点
- 剑指offer面试题63 二叉搜索树的第k个结点
- 算法系列——二叉搜索树的第k个结点