标签: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]
标签:class app HSM list() 第k小 元素 efi ini not
原文地址:https://www.cnblogs.com/cbachen/p/14927732.html