# 二叉搜索树的第k个节点📕

# 题目描述

给定一棵二叉搜索树,请找出其中的第k小的节点。 例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4。

# 解题思路

  1. 利用二叉搜索树中序遍历就是按从小到大的特性;
  2. 中序遍历排好序后返回对应k-1下标的那一项节点。

# coding

/*
* 二叉树结点类
*/
class Node {
    constructor(value = 0, left = null, right = null) {
        this.value = value;
        this.left = left;
        this.right = right;
    }
}
/**
* 二叉搜索树的第k个节点
* @param {Node} root
* @param {Number} target
 */
function KthNode(root, target) {
    const arr = []
    inOrderRec(root, arr)
    if (target > 0 && target <= arr.length) {
        return arr[target - 1]
    }
    return null;
}
/*
* 二叉树中序递归遍历
*/
function inOrderRec (node, arr) {
    if (node) {
        inOrderRec(node.left, arr)
        arr.push(node)
        inOrderRec(node.right, arr)
    }
}
/*
* 二叉树中序非递归遍历
*/
// function inOrderUnRec (node) {
//   let list = [];
//   let stack = [];
//   while (stack.length !== 0 || node) {
//     if (node) {
//       stack.push(node);
//       node = node.left;
//     } else {
//       node = stack.pop();
//       list.push(node);
//       node = node.right;
//     }
//   }
//   return list;
// }

const root = new Node(
  2,
  new Node(1),
  new Node(
    5,
    new Node(4),
    new Node(6)
  )
);
console.log(KthNode(root, 3))
// { value: 4, left: null, right: null }
阅读全文