From 03fd761b887120a7ec123faa282af163fbc76dbb Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:56:20 +0530 Subject: [PATCH 1/7] Create Tech-neophyte--c.md --- .../Tech-neophyte--c.md | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md diff --git a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md new file mode 100644 index 0000000..763e9d3 --- /dev/null +++ b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md @@ -0,0 +1,33 @@ + +## Approach +This C++ code defines a `Solution` class with a method `convertBST` that takes the root of a binary search tree (BST) and returns the same tree modified to be a Greater Tree. The `convertToGT` function recursively traverses the BST in reverse in-order, updating node values to the sum of greater values encountered so far. The final modified tree is returned. +## cpp Code +``` +/** + * 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: + TreeNode* convertToGT(TreeNode* root,int& sum){ + if(!root)return NULL; + convertToGT(root->right,sum); + sum+=root->val; + root->val=sum; + convertToGT(root->left,sum); + return root; + } + TreeNode* convertBST(TreeNode* root) { + int sum=0; + return convertToGT(root,sum); + } + +}; +``` From e7eb6a072ff7389fed67dcd92f336e16823e5482 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:56:39 +0530 Subject: [PATCH 2/7] Update Tech-neophyte--c.md --- .../Tech-neophyte--c.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md index 763e9d3..60a15b8 100644 --- a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md +++ b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md @@ -1,4 +1,16 @@ - +## Python code: +class Solution: + def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: + stack, summ, node = [], 0, root + while stack or node: + while node: + stack.append(node) + node = node.right + node = stack.pop() + node.val += summ + summ = node.val + node = node.left + return root ## Approach This C++ code defines a `Solution` class with a method `convertBST` that takes the root of a binary search tree (BST) and returns the same tree modified to be a Greater Tree. The `convertToGT` function recursively traverses the BST in reverse in-order, updating node values to the sum of greater values encountered so far. The final modified tree is returned. ## cpp Code From b54cac75fe7ad046c0436ca820d744648a7d4210 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:56:57 +0530 Subject: [PATCH 3/7] Rename Tech-neophyte--c.md to Tech-neophyte--pc.md --- .../{Tech-neophyte--c.md => Tech-neophyte--pc.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Day-25/Q2: Convert BST to Greater Tree/{Tech-neophyte--c.md => Tech-neophyte--pc.md} (100%) diff --git a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md similarity index 100% rename from Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--c.md rename to Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md From e045bfcb86f53cb747b8a5b2e7b5a470d536a76d Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:57:31 +0530 Subject: [PATCH 4/7] Update Tech-neophyte--pc.md --- Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md index 60a15b8..11b2ec0 100644 --- a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md +++ b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md @@ -1,4 +1,5 @@ ## Python code: +``` class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: stack, summ, node = [], 0, root @@ -11,6 +12,7 @@ class Solution: summ = node.val node = node.left return root +``` ## Approach This C++ code defines a `Solution` class with a method `convertBST` that takes the root of a binary search tree (BST) and returns the same tree modified to be a Greater Tree. The `convertToGT` function recursively traverses the BST in reverse in-order, updating node values to the sum of greater values encountered so far. The final modified tree is returned. ## cpp Code From 0f249f125dbe7596160890467a0b05d680d32414 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 19:26:29 +0530 Subject: [PATCH 5/7] Define approach for python --- Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md index 11b2ec0..59ee5e6 100644 --- a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md +++ b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md @@ -1,4 +1,4 @@ -## Python code: +## Python code: Using stack ``` class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: From 2f551b5d1742fe77d90c057f6ce17f5e45d503dd Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 19:27:54 +0530 Subject: [PATCH 6/7] Add another approach --- Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md index 59ee5e6..0faced9 100644 --- a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md +++ b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md @@ -1,4 +1,5 @@ -## Python code: Using stack +## Approach 1: Using stack +## Python code: ``` class Solution: def convertBST(self, root: Optional[TreeNode]) -> Optional[TreeNode]: @@ -13,6 +14,8 @@ class Solution: node = node.left return root ``` +## Approach 2: using recursive dfs +## python code: ## Approach This C++ code defines a `Solution` class with a method `convertBST` that takes the root of a binary search tree (BST) and returns the same tree modified to be a Greater Tree. The `convertToGT` function recursively traverses the BST in reverse in-order, updating node values to the sum of greater values encountered so far. The final modified tree is returned. ## cpp Code From 3826d5e8cea8ff0c4ef1df17c95c2dbf26bdddc2 Mon Sep 17 00:00:00 2001 From: Aananditaa <122295513+Tech-neophyte@users.noreply.github.com> Date: Sun, 28 Jan 2024 19:28:29 +0530 Subject: [PATCH 7/7] Update Tech-neophyte--pc.md --- .../Tech-neophyte--pc.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md index 0faced9..1cfee28 100644 --- a/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md +++ b/Day-25/Q2: Convert BST to Greater Tree/Tech-neophyte--pc.md @@ -16,6 +16,18 @@ class Solution: ``` ## Approach 2: using recursive dfs ## python code: +``` +def dfs(node, val): + if node.right: + val = dfs(node.right, val) + val += node.val + node.val = val + if node.left: + val = dfs(node.left, val) + return val + dfs(root, 0) + return root +``` ## Approach This C++ code defines a `Solution` class with a method `convertBST` that takes the root of a binary search tree (BST) and returns the same tree modified to be a Greater Tree. The `convertToGT` function recursively traverses the BST in reverse in-order, updating node values to the sum of greater values encountered so far. The final modified tree is returned. ## cpp Code