class Solution {
int id=0;
int target=0;
public int kthLargest(TreeNode root, int k) {
id=k;
rrl(root);
return target;
}
public void rrl(TreeNode root){
if(root==null||id<=0)return;
rrl(root.right);
id--;
if(id==0){
target=root.val;
}
rrl(root.left);
}
}
#include <iostream>
using namespace std;
// 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 kthLargest(TreeNode *root, int k)
{
postOrder(root, k);
return res;
}
int res = 0;
int rank = 0;
// 中序遍历,因为找第k大,所以需要倒序遍历
// 先遍历右子树,从右往左数第k大
void postOrder(TreeNode *root, int k)
{
if (root == nullptr)
return;
// 遍历右子树,直到达到最深处,即为最大开始往回找
postOrder(root->right, k);
++rank;
if (rank == k)
{
res = root->val;
return;
}
postOrder(root->left, k);
}
};
int main()
{
// 创建二叉树
// TreeNode* root = new TreeNode(5);
// root->left = new TreeNode(3);
// root->right = new TreeNode(6);
// root->left->left = new TreeNode(2);
// root->left->right = new TreeNode(4);
// root->left->left->left = new TreeNode(1);
TreeNode *root = new TreeNode(3);
root->left = new TreeNode(1);
root->right = new TreeNode(4);
root->left->right = new TreeNode(2);
Solution solution;
int k = 3; // 要找第k大的节点
int result = solution.kthLargest(root, k);
cout << "The " << k << "th largest element in the binary tree is: " << result << endl;
// 释放内存
// delete root->left->left->left;
// delete root->left->left;
delete root->left->right;
delete root->left;
delete root->right;
delete root;
return 0;
}
class Solution {
//二叉搜索树,中序遍历,按顺序排列!
int cnt = 0;
int res = 0;
public void InOrder(TreeNode root, int k){
if(root==null) return;
InOrder(root.right, k);
cnt++;
if(cnt==k){
res = root.val;
return;
}
InOrder(root.left, k);
}
public int kthLargest(TreeNode root, int k) {
InOrder(root, k);
return res;
}
}
共以下 32 个回答
“`java
中序遍历倒叙
class Solution {
int res;
int cnt;
public int findTargetNode(TreeNode root, int cnt) {
this.cnt = cnt;
recur(root);
return res;
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res=0,count=0;
public int kthLargest(TreeNode root, int k) {
count = k;
inorder(root);
return res;
}
void inorder(TreeNode root){
if(rootnull){
return;
}
inorder(root.right);
count–;
if(count0){
res = root.val;
}
inorder(root.left);
}
}
加油
“`java
时间复杂度和空间复杂度都为 O(n)
class Solution {
int k=0;
int target;
public int kthLargest(TreeNode root, int k) {
// 使用右根左
this.k=k;
right_root_left(root);
return target;
}
public void right_root_left(TreeNode root){
if(root==null||k<0){
return ;
}
right_root_left(root.right);
k--;
if(k==0){
target=root.val;
}
right_root_left(root.left);
}
}
【思路】递归思路,用中序遍历(右中左)即可得到二叉搜索树的从大到小排列
【复杂度】时间/空间复杂度均为平均O(logn),最差O(n)
时间复杂度O(N)
空间复杂度O(N)
这段代码的功能是在给定的二叉树中找到第k大的节点值。
代码首先定义了一个二叉树节点的结构体,包含值、左子节点和右子节点。然后定义了一个Solution类,其中有一个成员函数kthLargest,用于找到第k大的节点值。
kthLargest函数首先调用了postOrder函数,对二叉树进行倒序的中序遍历。具体来说,先遍历右子树,然后遍历当前节点,最后遍历左子树。在遍历右子树的过程中,每遍历一个节点,将rank值加1。当rank等于k时,说明找到了第k大的节点,将该节点的值赋给res,并返回。在遍历左子树的过程中,同样进行相同的操作。
在主函数中,创建了一个二叉树,并调用了Solution类的kthLargest函数,将要找的第k大的节点值存储在result变量中。最后输出结果。
代码中还包含了释放内存的操作,将创建的节点逐个删除。
注:代码中的二叉树创建部分被注释掉了,可以根据需要取消注释并修改节点的值来创建不同的二叉树。
时间复杂度是 O(N)
空间复杂度是 O(N)
空间复杂度和时间复杂度都是O(n)
迭代法实现
复杂度
Code
思路:二叉搜索树按照中序遍历,其节点值是从小到大的。不过我们要找第k大节点,只需要将“左根右”遍历顺序变为“右根左”即可。
时间复杂度:O(n) 空间复杂度:O(n)
中序遍历
//中序感觉还是有点绕
时间复杂度:O(N)
额外空间复杂度:O(N)
中序遍历的倒序:右中左
时间:O(n)
空间:O(n)
c++中序统一迭代vec;
int kthLargest(TreeNode* root, int k) {
vector
stack<TreeNode>st;
if(root!=NULL)st.push(root);
while(!st.empty())
{
TreeNode temp = st.top();
if(temp!=NULL)
{
st.pop();
if(temp->right)st.push(temp->right);
st.push(temp);
st.push(NULL);
if(temp->left)st.push(temp->left);
}
else
{
st.pop();
temp = st.top();
st.pop();
vec.push_back(temp->val);
}
}
return vec[vec.size()-k];
}
记得helper function
C++实现
根据中序遍历有序的特点。
时空复杂度:O(n)
1、还是分析发现需要递归找到最大的节点,也就是右子树中最右边的节点
2、递归三部曲:确定递归函数的方法体,确定返回值,理清栈中递归前后的逻辑
3、第三点相对较难,可以思考到从最底层栈中返回时,递归函数返回的就是最大值,我们只用计数,然后返回等于零时的值
时间复杂度:O(n)
空间复杂度:O(1)
复杂度O(n)
时间复杂度,空间复杂度:O(n) 最差O(logn)
二叉搜索树的中序遍历是从小到大的数组,所以反过来是从大到小
每遍历一次k值减一,直到为0则此时结点为目标值
C++实现:
class Solution {
public:
vector res;
void order(TreeNode* root)
{
if(root NULL) return;
order(root->left);
res.push_back(root->val);
order(root->right);
}
int kthLargest(TreeNode* root, int k)
{
if(root NULL) return -1;
order(root);
return res[res.size() – k];
}
};
时间复杂度:最差为O(n),平均为logn
空间复杂度:最差为O(n),平均为logn
MY