leetcode-python-二叉搜索树中第K小的元素

2021-08-16 09:59

阅读:779

标签:class   app   HSM   list()   第k小   元素   efi   ini   not   逐行入栈,排序后直接输出第k小的数据 # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def kthSmallest(self, root: TreeNode, k: int) -> int: if not root: return None allTree = [root] allVal = list() while allTree: p = allTree[0] allVal.append(p.val) if p.left: allTree.append(p.left) if p.right: allTree.append(p.right) del allTree[0] allVal.sort() return allVal[k-1]  leetcode-python-二叉搜索树中第K小的元素标签:class   app   HSM   list()   第k小   元素   efi   ini   not   原文地址:https://www.cnblogs.com/cbachen/p/14927732.html


评论


亲,登录后才可以留言!