From f6d0ab40b352bcf58e540b1878f85c57b7a006ef Mon Sep 17 00:00:00 2001 From: pankajm18 <43561999+pankajm18@users.noreply.github.com> Date: Tue, 27 Oct 2020 23:29:21 +0530 Subject: [PATCH] Create Greater Sum BST Solution of greater sum BST problem of leetcode. --- Greater Sum BST | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Greater Sum BST 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; + + } +};