二叉搜索树的第k大节点 cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int res;
void dfs(TreeNode* root, int &k) {
if (!root) return;
dfs(root->right, k);
k --;
if (k == 0) {
res = root->val;
return;
}
dfs(root->left, k);
}
int kthLargest(TreeNode* root, int k) {
dfs(root, k);
return res;
}
class Solution {
//BST的中序遍历递增
List<Integer> list = new ArrayList<>();
public int kthLargest(TreeNode root, int k) {
inOrder(root);
return list.get(list.size()-k);
}
public void inOrder(TreeNode root) {
if (root==null)
return;
inOrder(root.left);
list.add(root.val);
inOrder(root.right);
}
}
function kthLargest(root: TreeNode | null, k: number): number {
let cur = 1;
let res = root.val;
function dfs(node: TreeNode | null): void {
if(!node) return;
dfs(node.right);
if(cur++ === k) res = node.val;
dfs(node.left);
}
dfs(root);
return res;
};
”’java
class Solution {
public int kthLargest(TreeNode root, int k) {
//二叉搜素:中序遍历
List list = new ArrayList<>();
inOrder(root,list);
return list.get(k-1);
}
class Solution {
List list =new ArrayList();
public int kthLargest(TreeNode root, int k) {
//二叉搜索树 前序遍历就是递增的
bianli(root);
return list.get(k-1);
}
public void bianli(TreeNode root)
{
if(root null)
{
return ;
}
class Solution {
int res = 0, cnt = 0;
public int kthLargest(TreeNode root, int k) {
dfs(root,k);
return res;
}
public void dfs(TreeNode root, int k) {
if(root == null) return;
dfs(root.right,k);
if(++ cnt == k) {
res = root.val;
}
dfs(root.left,k);
}
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
//时间空间复杂度都是 O(N)
var K int
var res int
func kthLargest(root *TreeNode, k int) int {
K = k
dfs(root)
return res
}
func dfs(root *TreeNode) {
if root == nil {
return
}
dfs(root.Right)
if K == 0 {
return
}
K--
if K == 0 {
res = root.Val
}
dfs(root.Left)
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int cnt=0;
int ans;
public int kthLargest(TreeNode root, int k) {
inOrder(root,k);
return ans;
}
public void inOrder(TreeNode root,int k){
if(root==null) return;
inOrder(root.right,k);
cnt++;
if(cnt==k) {
ans=root.val;
return;
}
inOrder(root.left,k);
}
}
十
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res, k;
public int kthLargest(TreeNode root, int k) {
this.k = k;
dfs(root);
return res;
}
void dfs(TreeNode root) {
if (root null || k 0) return;
dfs(root.right);
if (–k 0) res = root.val;
dfs(root.left);
}
}
“`c++
//二叉搜索树的中序遍历是递增的
class Solution {
public:
int count=0;
int kthLargest(TreeNode* root, int k) {
if (!root) return -1;
int r = kthLargest(root->right, k);
count++;
if (count == k) return root->val;
int l = kthLargest(root->left, k);
return l == -1 ? r : l;
}
};
共以下 32 个回答
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
//遍历+对每个结点进行判断,判断时机参考中序遍历,k–,当k = 0时即跳出循环不再继续,保留循环之前的res
//递归,为了不让res和cur空间因为递归调用结束被回收,故定义为全局变量
//
class Solution {
private:
int res;
int cur;
public:
int kthLargest(TreeNode* root, int k) {
cur = k;
check(root, k);
return res;
}
};
二叉搜索树的第k大节点 cpp
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
};
递归逆序中序遍历
ts
时间复杂度:平均O(logn) 最差 O(n)
空间复杂度:平均O(logn) 最差 O(n)
O(n)
思路:中序遍历二叉搜索树得到是一个递增的序列,直接可得第k大元素的值是多少
c++ 代码 中序遍历为有序数组 v;
class Solution {
vector
int count;
public:
int kthLargest(TreeNode* root, int k)
{
dfs(root);
return v.at(v.size()-k);
}
void dfs(TreeNode* root)
{
if(!root) return;
};
二叉搜索树中序遍历即为正序排序结果, 那么中序遍历即可。 双O(n)
中序遍历
代码
简单题
”’java list = new ArrayList<>();
class Solution {
public int kthLargest(TreeNode root, int k) {
//二叉搜素:中序遍历
List
inOrder(root,list);
return list.get(k-1);
}
}
时间复杂度O(n)
空间复杂度O(n)
”’
class Solution { list =new ArrayList ();
List
public int kthLargest(TreeNode root, int k) {
//二叉搜索树 前序遍历就是递增的
bianli(root);
return list.get(k-1);
}
public void bianli(TreeNode root)
{
if(root null)
{
return ;
}
}
深度优先遍历
Definition for a binary tree node.
class TreeNode:
def init(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def kthLargest(self, root: TreeNode, k: int) -> int:
def dfs(root):
if not root: return
dfs(root.right)
if self.k 0: return
self.k -= 1
if self.k 0: self.res = root.val
dfs(root.left)
第k大,是右根左
时间复杂度:O(n)
空间复杂度:O(n)
时间复杂度:O(n);
空间复杂度:O(1);
时间复杂度O(n),空间复杂度O(n)
思路
首先是BST,特性就是左子树和右子树的所有节点都分别小于或者大于root。所以为了得到递增的效果,需要用中序遍历的方法。
代码
改进
但是这样用了一个vector,然而除了第k大的数都用不上,所以为了降低空间复杂度,可以用一个参数来指定输出结果。
因为不能统计整个vector的数量,所以可以稍微改一下中序遍历的输出,改为右中左的顺序,这样的输出结果就是一个降序的过程,可以直接得到第k大的数。
练习
把中序遍历用非递归的方法来一次。
不用vector来辅助,直接用一个int来记录结果
十
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res, k;
public int kthLargest(TreeNode root, int k) {
this.k = k;
dfs(root);
return res;
}
void dfs(TreeNode root) {
if (root null || k 0) return;
dfs(root.right);
if (–k 0) res = root.val;
dfs(root.left);
}
}
“`c++
//二叉搜索树的中序遍历是递增的
class Solution {
public:
int count=0;
int kthLargest(TreeNode* root, int k) {
if (!root) return -1;
int r = kthLargest(root->right, k);
count++;
if (count == k) return root->val;
int l = kthLargest(root->left, k);
return l == -1 ? r : l;
}
};
7.17
Day10:下面是代码
时间复杂度:O(n)
空间复杂度:O(n)