求:
给出二叉 搜索 树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。
提醒一下,二叉搜索树满足下列约束条件:
节点的左子树仅包含键 小于 节点键的节点。
节点的右子树仅包含键 大于 节点键的节点。
左右子树也必须是二叉搜索树。
示例:
输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8]
输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8]
提示:
树中的节点数介于 1 和 100 之间。
每个节点的值介于 0 和 100 之间。
给定的树为二叉搜索树。
注意:该题目与 538: https://leetcode-cn.com/problems/convert-bst-to-greater-tree/ 相同
解:
思路:反向中序遍历,记录前面出现的所有节点的值的和,加入到当前节点。递归操作完成后,返回根节点的指针。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
void
convert(
struct
TreeNode* root,
int
* sum){
if
(root==NULL)
return
;
convert(root->right,sum);
*sum += root->val;
root->val = *sum;
convert(root->left,sum);
}
struct
TreeNode* bstToGst(
struct
TreeNode* root){
if
(root==NULL)
return
root;
int
sum =
0
;
convert(root,&sum);
return
root;
}
来源:oschina
链接:https://my.oschina.net/u/4469818/blog/4284095