diff --git a/Greater Sum BST b/Greater Sum BST new file mode 100644 index 0000000..122c513 --- /dev/null +++ b/Greater Sum BST @@ -0,0 +1,37 @@ +/** + * 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: + vectorv; + vectorsum; + void inorder(TreeNode* root) + { + if(root->left!=NULL) + inorder(root->left); + v.push_back(root); + sum.push_back(root->val); + if(root->right!=NULL) + inorder(root->right); + } + TreeNode* bstToGst(TreeNode* root) { + if(root==NULL) + return root; + inorder(root); + int n=v.size(); + for(int i=n-2;i>=0;i--) + sum[i]+=sum[i+1]; + for(int i=0;ival=sum[i]; + return root; + + } +};