LeetCode C++ 1038. Binary Search Tree to Greater Sum Tree【二叉搜索树】中等
发布日期:2021-07-01 02:51:51
浏览次数:2
分类:技术文章
本文共 2624 字,大约阅读时间需要 8 分钟。
Given the root of a binary search tree with distinct values, modify it so that every node has a new value equal to the sum of the values of the original tree that are greater than or equal to node.val
.
As a reminder, a binary search tree is a tree that satisfies these constraints:
- The left subtree of a node contains only nodes with keys less than the node’s key.
- The right subtree of a node contains only nodes with keys greater than the node’s key.
- Both the left and right subtrees must also be binary search trees.
Example 1:
Input: [4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]Output: [30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
Constraints:
- The number of nodes in the tree is between
1
and100
. - Each node will have value between
0
and100
. - The given tree is a binary search tree.
题意:给出二叉搜索树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node
的新值等于原树中大于或等于 node.val
的值之和。
思路1:我们需要以有序的形式从大到小遍历二叉搜索树,逐渐累加结点的值,赋给当前结点。下面的代码是递归中序遍历(左根右)的变形(右根左):
class Solution { public: int sum = 0; TreeNode* bstToGst(TreeNode* root) { if (root) { bstToGst(root->right); sum += root->val; root->val = sum; bstToGst(root->left); } return root; } };
思路2:非递归中序遍历。代码如下:
class Solution { public: TreeNode* bstToGst(TreeNode* root) { stackst; TreeNode *temp = root; int sum = 0; while (!st.empty() || temp) { while (temp) { st.push(temp); temp = temp->right; } temp = st.top(); st.pop(); sum += temp->val; temp->val = sum; temp = temp->left; } return root; }};
思路3:非递归中序遍历,使用 stack
模拟系统栈的函数调用:
class Solution { public: struct command { int instruction; //为0时下次访问结点,为1时下次遍历该结点代表的子树(相当于递归函数调用访问子树) TreeNode *root; command(int i = 0, TreeNode *rt = nullptr) : instruction(i), root(rt) { } }; TreeNode* bstToGst(TreeNode* root) { if (root == nullptr) return root; stackst; st.push(command(1, root)); int sum = 0; while (!st.empty()) { const command temp = st.top(); st.pop(); if (temp.instruction == 0) { //访问结点 sum += temp.root->val; temp.root->val = sum; } else { //访问结点代表的子树(右-根-左) TreeNode *t = temp.root; if (t->left) st.push(command(1, t->left)); st.push(command(0, t)); if (t->right) st.push(command(1, t->right)); } } return root; }};
转载地址:https://memcpy0.blog.csdn.net/article/details/108722883 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年05月04日 19时40分31秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
Unix环境变量--缓冲区
2019-05-02
Unix环境变量--堆和栈的区别
2019-05-02
Unix环境变量--POSIX异步I/O
2019-05-02
UNIX环境变量--读写函数变体
2019-05-02
UNIX环境变量--存储映射I/O
2019-05-02
UNIX环境变量--IPC之管道通信
2019-05-02
C++虚继承【详解】
2019-05-02
tinyhttpd源码学习1
2019-05-02
Plus One
2019-05-02
Linux内核完全剖析0.12(一)
2019-05-02
Sum Root to Leaf Numbers
2019-05-02
Pascal's Triangle
2019-05-02
ffmpeg提取音频存为PCM
2019-05-02
我也学android(1)搭个环境
2019-05-02
Reverse Linked List II
2019-05-02
最长递增子序列
2019-05-02
题目1511:从尾到头打印链表
2019-05-02
Web Bench 源码学习1
2019-05-02
Search Insert Position
2019-05-02
Length of Last Word
2019-05-02