#力扣 LeetCode 劍指 Offer 54. 二叉搜尋樹的第k大節點 @FDDLC
阿新 • • 發佈:2020-12-28
技術標籤:演算法&資料結構
題目描述:
https://leetcode-cn.com/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/
Java程式碼:
class Solution { // ≤ k ≤ 二叉搜尋樹元素個數 int answer,k; public void go(TreeNode root){ if(root.right!=null)go(root.right); if(--k==0)answer=root.val; if(k>0&&root.left!=null)go(root.left); } public int kthLargest(TreeNode root, int k) { //右中左遍歷 this.k=k; go(root); return answer; } }