二叉搜索树的第K大节点

题目:

输入一个颗二叉树搜索树,请找出其中的第K大节点。

解答:

 1 public class Solution {
 2 
 3     public TreeNode kthNode(TreeNode root, int k) {
 4         if(root == null || k == 0) {
 5             return null;
 6         }
 7 
 8         return kthNodeCore(root, k);
 9     }
10 
11     private static TreeNode kthNodeCore(TreeNode root, int k) {
12         TreeNode target = null;
13         if(root.left != null) {
14             target = kthNodeCore(root.left, k);
15         }
16 
17         if(target == null) {
18             if(k == 1) {
19                 target = root;
20             }
21 
22             k--;
23         }
24 
25         if(target == null && root.right != null) {
26             target = kthNodeCore(root.right, k);
27         }
28 
29         return target;
30     }
31 }

原文地址:https://www.cnblogs.com/wylwyl/p/10475252.html