东哥带你刷二叉搜索树(特性篇) :: labuladong的算法小抄 #988
Replies: 22 comments 10 replies
-
如何让每一个节点知道自己的排名?每个节点需要记录,以自己为根的这棵二叉树有多少个节点。有了 size 字段,外加 BST 节点左小右大的性质,对于每个节点 node 就可以通过 node.left 推导出 node 的排名 这一段表述不太理解,知道以自己为根的这棵二叉树有多少个节点后 对计算此节点的排名有什么帮助? |
Beta Was this translation helpful? Give feedback.
-
BST 左小右大,所以节点 |
Beta Was this translation helpful? Give feedback.
-
那算第k小的节点的时候,每次对比当前节点的时候,都得去计算出左子树节点的个数和总子树个数么? |
Beta Was this translation helpful? Give feedback.
-
@zxc948406613 如果能够把节点的个数存到 TreeNode 里面,直接就可以拿到 |
Beta Was this translation helpful? Give feedback.
-
@labuladong 那如果是叶子节点没有左子树应该怎么推导 |
Beta Was this translation helpful? Give feedback.
-
分解问题的思路: TreeNode* convertBST(TreeNode* root) {
SumTree(root,0);
return root;
}
int SumTree(TreeNode* root,int right){
if(!root) return 0;
int RightSum=SumTree(root->right,right);
int LeftSum=SumTree(root->left,right+root->val+RightSum);
int original_root_val=root->val;
root->val+=RightSum+right;
return LeftSum+RightSum+original_root_val;
} |
Beta Was this translation helpful? Give feedback.
-
东哥yyds, 看了核心框架篇后刷了些数组和链表题,就立马来刷二叉树了,逐渐有点感觉了 |
Beta Was this translation helpful? Give feedback.
-
滴滴滴打卡 |
Beta Was this translation helpful? Give feedback.
-
BST 转化累加树中的递归函数,为什么要把 sum 作为外部累加变量,而不能是递归函数的一个参数? |
Beta Was this translation helpful? Give feedback.
-
打卡打卡 比上一篇简单多了! |
Beta Was this translation helpful? Give feedback.
-
打卡第一题,中序遍历存储后返回索引为k-1的即可 class Solution {
private:
vector<int> res;
public:
int kthSmallest(TreeNode* root, int k) {
traverse(root);
return res[k - 1];
}
void traverse(TreeNode* root)
{
if (root == nullptr) return;
traverse(root->left);
res.push_back(root->val);//BST中序遍历,在这里保存一个数值
traverse(root->right);
return;
}
}; |
Beta Was this translation helpful? Give feedback.
-
the key is the inorder slice which is ascend order, here is the solution of go version /**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func kthSmallest(root *TreeNode, k int) int {
res := dfs(root)
fmt.Println(res)
for i, v := range res {
if i == k - 1 {return v}
}
return 0
}
func dfs(root *TreeNode) []int {
res := []int{}
if root == nil {return res}
res = append(res, dfs(root.Left)...)
res = append(res, root.Val)
res = append(res, dfs(root.Right)...)
return res
} |
Beta Was this translation helpful? Give feedback.
-
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func convertBST(root *TreeNode) *TreeNode {
sum := 0
traverse(root, &sum)
return root
}
func traverse(root *TreeNode, sum *int) {
if root == nil {return}
traverse(root.Right, sum)
*sum += root.Val
root.Val = *sum
traverse(root.Left, sum)
} |
Beta Was this translation helpful? Give feedback.
-
2022.11.2 打卡 |
Beta Was this translation helpful? Give feedback.
-
这个地方:如果 k > m,那说明排名第 k 的元素在右子树,所以可以去右子树搜索第 k - m - 1 个元素。 |
Beta Was this translation helpful? Give feedback.
-
请问一下“对于一个节点来说,确实右子树都是比它大的元素,但问题是它的父节点也可能是比它大的元素呀”这里是什么意思啊 |
Beta Was this translation helpful? Give feedback.
-
第230题 类似于增加size字段的方法
|
Beta Was this translation helpful? Give feedback.
-
230的解法可以做一点优化:在
可以加个分支
这样可以避免在找到想要的元素之后及时返回,而不是又继续往右子树遍历。 |
Beta Was this translation helpful? Give feedback.
-
01092023 Check |
Beta Was this translation helpful? Give feedback.
-
打卡 |
Beta Was this translation helpful? Give feedback.
-
当我自己去创建一个累加树的时候,规律就被发现了,很神奇。我开始也是打算保存父节点且考虑左右子节点分别处理,后面自己在图上画画累加树就看见规律了,哈哈,发一下癫! |
Beta Was this translation helpful? Give feedback.
-
为啥两个题目,一模一样,奇怪。 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
文章链接点这里:东哥带你刷二叉搜索树(特性篇)
评论礼仪 见这里,违者直接拉黑。
Beta Was this translation helpful? Give feedback.
All reactions