寻找二叉搜索树中的目标节点 (寻找二叉搜索树中的目标节点)

 

思路:

// @Title: 寻找二叉搜索树中的目标节点 (寻找二叉搜索树中的目标节点)
// @Author: qisiii
// @Date: 2022-03-01 21:24:07
// @Runtime: 2 ms
// @Memory: 41.5 MB
// @comment: 
// @flag: 
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int kthLargest(TreeNode root, int k) {
            List<Integer> shuzu=inorderTraversal(root);
            
            return shuzu.get(shuzu.size()-k);
    }
    public List<Integer> inorderTraversal(TreeNode root) {
        if(root==null){
            return new ArrayList();
        }
        List<Integer> zhong=new ArrayList();
        zhong.addAll(inorderTraversal(root.left));
        zhong.add(root.val);
        zhong.addAll(inorderTraversal(root.right));
        return zhong;
    }
}
Licensed under CC BY-NC-SA 4.0
最后更新于 2024-10-18