java
class Solution {
LinkedList list = new LinkedList<>();
public int findTargetNode(TreeNode root, int k) {
dfs(root);
return list.get(list.size() – k);
}
“`java
class Solution {
public Integer count = 0;
public Integer result = null;
public Boolean isFound = false;
public int findTargetNode(TreeNode root, int cnt) {
this.count = cnt;
middleSearch(root);
return result;
}
class Solution {
public int findTargetNode(TreeNode root, int cnt) {
List<Integer> result = new ArrayList<>();
inorder(result,root);
return result.get(result.size()-cnt);
}
public void inorder(List<Integer> result,TreeNode root){
if(root==null){
return;
}
inorder(result,root.left);
result.add(root.val);
inorder(result,root.right);
}
}
思路:二叉搜索树的左子树节点val都比根节点小,右子树节点都比根节点val大,因此二叉搜索树的中序遍历序列刚好就是从小到大排列的,所以先获取到中序遍历的结果,然后再返回倒数第cnt的值
时间复杂度:O(n),遍历了树的所有节点
空间复杂度:O(n),用了一个额外的动态数组来存储中序遍历的结果。
共以下 28 个回答
class Solution {
int res,cnt;
public int findTargetNode(TreeNode root, int cnt) {
this.cnt = cnt;
dfs(root);
return res;
}
}
思路:中序遍历的倒序,获取第cnt个遍历的节点的值即为第k大节点
java list = new LinkedList<>();
class Solution {
LinkedList
public int findTargetNode(TreeNode root, int k) {
dfs(root);
return list.get(list.size() – k);
}
}
提交
class Solution {
public:
int findTargetNode(TreeNode* root, int cnt) {
this->cnt = cnt;
dfs(root);
return res;
}
private:
int res, cnt;
void dfs(TreeNode* root) {//倒序,中序遍历,递减数列
if(root nullptr) return;
dfs(root->right);
if(cnt 0) return;
if(–cnt 0) res = root->val;
dfs(root->left);
}
};
class Solution {
public:
int findTargetNode(TreeNode* root, int cnt) {
if(root nullptr) return 0;
this->cnt = cnt;
f(root);
private:
int res;
int cnt;
};
思路:二叉搜索树的中序遍历为(左中右)为递减序列,那么(右中左)为递增序列
时间、空间复杂度O(n)
时间复杂度:最差为 O(n),平均为 logn
空间复杂度:最差为 O(n),平均为 logn
class Solution {& v, TreeNode* root){ v;
public:
void dfs(vector
if (root nullptr) return ;
dfs(v, root->left);
v.push_back(root->val);
dfs(v, root->right);
}
int findTargetNode(TreeNode* root, int cnt) {
vector
dfs(v, root);
return v[v.size() – cnt];
}
};注意,二叉搜索树的节点关系。左<根<右—符合中序遍历,返回中序遍历的倒数第几个节点就是
第二次打卡二叉搜索树
int res,cnt;
public int findTargetNode(TreeNode root, int cnt) {
this.cnt=cnt;
dfs(root);
return res;
}
void dfs(TreeNode root){
if(rootnull) return;
dfs(root.right);
if(cnt0) return;
if(–cnt0) res=root.val;
dfs(root.left);
}
思路:二叉搜索树的中序遍历是递增的 反过来就是递减
找到第n个返回即可
二叉搜索树本质上是中序遍历建构的结果,中序遍历前面小,后面大,只需要按照中序遍历的方向反过来就可以了。
辅助栈反推法
时间复杂度$O(n)$(理论,实际上由于栈的内部过程是要比递归法慢的),空间复杂度$O(n)$
递归法
时间复杂度$O(n)$,额外空间复杂度$O(n)$(递归栈)
时间复杂度O(n)
空间复杂度O(n)
中序遍历之后是一个有序的从小到大的数组,然后数组大小-cnt即可
“`C++ sub; result;
class Solution {
public:
int findTargetNode(TreeNode* root, int cnt) {
if(root == nullptr)
{
return -1;
}
//中序遍历
stack
vector
TreeNode* cur = root;
do
{
while(cur != nullptr)
{
sub.push(cur);
cur = cur->left;
}
//出栈时节点存入数组容器
if(sub.empty() == false)
{
cur = sub.top();
result.push_back(cur->val);
cur = cur->right;
sub.pop();
}
}while(cur != nullptr || sub.empty() == false);
};
“`
BST性质
https://leetcode.cn/problems/er-cha-sou-suo-shu-de-di-kda-jie-dian-lcof/solutions/2681846/xun-zhao-er-cha-sou-suo-shu-zhong-de-mu-5g1eb
打卡
Definition for a binary tree node.
class TreeNode:
def init(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def findTargetNode(self, root: Optional[TreeNode], cnt: int) -> int:
def dfs(root):
if not root:return
dfs(root.right)
self.cnt -= 1
if self.cnt 0: self.res = root.val
dfs(root.left)
双 on
使用递归,使用中序遍历后,二叉树的数据就是顺序数据。然后使用栈保存,取出倒数cnt的数据
day10第一题
//方法1 递归
class Solution {
HashMap<Integer,Integer> map = new HashMap<>();
public int findTargetNode(TreeNode root, int cnt) {
if(cnt count(root.right) + 1){
return root.val;
}
else if(cnt > count(root.right) + 1){
return findTargetNode(root.left,cnt – count(root.right) – 1);
}
else
return findTargetNode(root.right,cnt);
} stack = new Stack<>();
//方法2 类中序遍历
class Solution {
public int findTargetNode(TreeNode root, int cnt) {
//中序遍历
int count = 0;
Stack
while(root!=null || !stack.empty()){
while(root != null){
stack.push(root);
root = root.right;
}
root = stack.pop();
count++;
if(count cnt) return root.val;
root = root.left;
if(count cnt) return root.val;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int findTargetNode(TreeNode root, int cnt) {
if(root nullptr) return -1;
this->cnt = cnt;
dfs(root);
return res;
}
private:
int cnt = 0, res = 0;
void dfs(TreeNode* root){
if(!root) return;
dfs(root->right);
if(cnt 0) return;
else {
–cnt;
res = root->val;
}
dfs(root->left);
}
};
时间复杂度O(n),最坏空间复杂度O(n)
时空复杂度O(N)
“`java
class Solution {
public Integer count = 0;
public Integer result = null;
public Boolean isFound = false;
public int findTargetNode(TreeNode root, int cnt) {
this.count = cnt;
middleSearch(root);
return result;
}
}
“` 左中右遍历是递增,右中左是递减。