题解 | #二叉搜索树的第k个节点#

二叉搜索树的第k个节点

https://www.nowcoder.com/practice/57aa0bab91884a10b5136ca2c087f8ff

function KthNode( proot ,  k ) {
    // write code here
  if(!proot || k <1) return -1
  let queue = [proot]
  let array = [proot]
  while(queue.length){
    let node = queue.shift()
    if(node.left){
      queue.push(node.left)
      array.push(node.left)
    }
    if(node.right){
      queue.push(node.right)
      array.push(node.right)
    }
  }
  array.sort((a,b) => a.val - b.val)
  
  if(k > array.length) return -1
  return array[k-1]['val'] || -1

}

全部评论

相关推荐

07-18 14:03
门头沟学院 Java
点赞 评论 收藏
分享
06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务