Jmeter系列(10)- 控制器Controllers、取樣器 Samplers、Logic Controllers邏輯控制器的入門介紹
阿新 • • 發佈:2020-12-17
這裡要注意題目的說法,課本、牛客 都是第K個節點 ,LeetCode 上說的是第K大的節點。大家都知道 二叉搜尋樹的中序遍歷
可以得到一個遞增序列,那麼 第K個, 和第K大 是稍稍不一樣的。
LeetCode
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution {int k; int target; public int kthLargest(TreeNode root, int k) { this.k = k ; dfs(root); return target; } void dfs(TreeNode root){ if(root == null) return; dfs(root.right); if(k == 0) return; if(--k == 0) target= root.val; dfs(root.left); } }
牛客
/* public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { TreeNode res; int count = 0; TreeNode KthNode(TreeNode pRoot,int k) { if(pRoot == null || k <= 0){ return null; } dfs(pRoot,k); return res; } void dfs(TreeNode root, int k){ if(count < k && root.left != null){ dfs(root.left,k); } if(++count == k){ res = root; return; } if(count < k && root.right != null){ dfs(root.right,k); } } }