package com.tree;
public class BinarySearchKth {
public static TreeNode getBinarySearchKth(TreeNode root, int k) {
//中序遍历到第k个返回
TreeNode target = null;
if (root.left != null) {
target = getBinarySearchKth(root.left, k);
}
if (target == null) {
if (k == 1) {
return target;
} else {
k--;
}
}
if (target == null && root.right != null) {
target = getBinarySearchKth(root.right,k);
}
return target;
}
private class TreeNode {
private TreeNode left;
private TreeNode right;
private Integer val;
public TreeNode(Integer val) {
this.val = val;
}
}
}
找搜索二叉树中第k大的值
最新推荐文章于 2022-04-29 11:01:16 发布