Given therootof a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus the sum of all keys greater than the original key in BST. As a
使用一个vector将所有节点的值以升序排列。然后比较,求和,加上。 3、代码 1TreeNode* convertBST(TreeNode*root) {2if(root ==NULL)3returnNULL;45vector<int>v;6inorder(root,v);7increaseVal(root, v);89returnroot;1011}1213voidincreaseVal(TreeNode *root, vector<int> &v)14{15if(root ==NULL)...
publicTreeNode convertBST3(TreeNode root) { TreeNode cur = root; int sum =0;while(cur !=null) {if(cur.right ==null) { sum += cur.val; cur.val= sum; cur = cur.left; }else{ TreeNode prev = cur.right;while(prev.left !=null&& prev.left != cur) { prev = prev.left; }if...
今天介绍的是LeetCode算法题中Easy级别的第122题(顺位题号是538)。给定二进制搜索树(BST),将其转换为更大树,使原始BST的每个键都更改为原始键加上所有键的总和大于BST中的原始键。例如: 输入:二进制搜索树的根,如下所示: 5 / \ 2 13 1 2 3 输出:大树的根,如下所示: 18 / \ 20 13 1 2 3 本次...
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like this: ...
538. Convert BST to Greater Tree* https://leetcode.com/problems/convert-bst-to-greater-tree/ 题目描述 Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater ...
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example:Input:The root of a Binary Search Tree likethis:5/\213Output:The root of a Grea...
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input:The root of a Binary Search Tree likethis:5/\213Output:The root of a Gr...
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input:The root of a Binary Search Tree like this: ...
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like this: ...