/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ classSolution{ publicintkthLargest(TreeNode root, int k){ List<Integer> list = new ArrayList<>(); inOrder(root, list); return list.get(list.size() - k); }
publicintkthLargest(TreeNode root, int k){ if (root == null) return0; int right = kthLargest(root.right, k); if (++cnt == k) { return root.val; } int left = kthLargest(root.left, k); return Math.max(left, right); } }