Welcome to Subscribe On Youtube
Formatted question description: https://leetcode.ca/all/538.html
538. Convert BST to Greater Tree
Level
Easy
Description
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:
5
/ \
2 13
Output: The root of a Greater Tree like this:
18
/ \
20 13
Solution
Same but just left/right mirror of https://leetcode.ca/2016-03-03-94-Binary-Tree-Inorder-Traversal
Begin by obtaining the inorder traversal of the binary search tree. This traversal lists all the nodes of the tree in ascending order based on their values.
Next, iterate over the inorder traversal in reverse order, skipping the last node (which corresponds to the highest value node in the original tree). For each node, find its immediate successor in the inorder traversal (i.e., the next node in the forward direction) and add its value to the node’s own value. After this operation, the node’s value will be updated to its original value plus the sum of all values greater than its own value in the original tree.
Finally, return the modified root node of the binary search tree.
Appended Solution 2: Translated Upstream Explanation
Morris traversal does not require the use of a stack, the time complexity is $O(n)$, and the space complexity is $O(1)$. The core idea is:
Definition s represents the cumulative sum of node values in a binary search tree. Traverse binary tree nodes:
- If the right subtree of the current node root is empty, add the current node value to s, update the current node value to s, and update the current node to
root.left. - If the right subtree of the current node root is not empty, find the leftmost node next of the right subtree (that is, the successor node of the root node under in-order traversal):
- If the left subtree of the successor node next is empty, point the left subtree of the successor node to the current node root, and update the current node to
root.right. - If the left subtree of the successor node next is not empty, add the current node value to s, update the current node value to s, then point the left subtree of the successor node to empty (that is, cancel the pointing relationship between next and root), and update the current node to
root.left.
- If the left subtree of the successor node next is empty, point the left subtree of the successor node to the current node root, and update the current node to
- Repeat the above steps until the binary tree node is empty and the traversal ends.
- Finally, return to the root node of the binary search tree.
Morris reverse-order in-order traversal has the same idea as Morris in-order traversal, except that the “left root right” of mid-order traversal is changed to “right root left”.
-
public class Convert_BST_to_Greater_Tree { /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { int sum = 0; public TreeNode convertBST(TreeNode root) { if (root == null) { return null; } convertBST(root.right); sum += root.val; root.val = sum; convertBST(root.left); return root; } } } ############ /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode convertBST(TreeNode root) { int s = 0; TreeNode node = root; while (root != null) { if (root.right == null) { s += root.val; root.val = s; root = root.left; } else { TreeNode next = root.right; while (next.left != null && next.left != root) { next = next.left; } if (next.left == null) { next.left = root; root = root.right; } else { s += root.val; root.val = s; next.left = null; root = root.left; } } } return node; } } // Solution 2 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode convertBST(TreeNode root) { int s = 0; TreeNode node = root; while (root != null) { if (root.right == null) { s += root.val; root.val = s; root = root.left; } else { TreeNode next = root.right; while (next.left != null && next.left != root) { next = next.left; } if (next.left == null) { next.left = root; root = root.right; } else { s += root.val; root.val = s; next.left = null; root = root.left; } } } return node; } } -
// OJ: https://leetcode.com/problems/convert-bst-to-greater-tree/ // Time: O(N) // Space: O(H) class Solution { int sum = 0; public: TreeNode* convertBST(TreeNode* root) { if (!root) return nullptr; convertBST(root->right); root->val = (sum += root->val); convertBST(root->left); return root; } }; // Solution 2 /** * 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* convertBST(TreeNode* root) { int s = 0; TreeNode* node = root; while (root) { if (root->right == nullptr) { s += root->val; root->val = s; root = root->left; } else { TreeNode* next = root->right; while (next->left && next->left != root) { next = next->left; } if (next->left == nullptr) { next->left = root; root = root->right; } else { s += root->val; root->val = s; next->left = nullptr; root = root->left; } } } return node; } }; -
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def convertBST(self, root: TreeNode) -> TreeNode: def dfs(root): nonlocal s if root is None: return dfs(root.right) s += root.val root.val = s dfs(root.left) s = 0 dfs(root) return root -
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func convertBST(root *TreeNode) *TreeNode { s := 0 node := root for root != nil { if root.Right == nil { s += root.Val root.Val = s root = root.Left } else { next := root.Right for next.Left != nil && next.Left != root { next = next.Left } if next.Left == nil { next.Left = root root = root.Right } else { s += root.Val root.Val = s next.Left = nil root = root.Left } } } return node } // Solution 2 /** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func convertBST(root *TreeNode) *TreeNode { s := 0 node := root for root != nil { if root.Right == nil { s += root.Val root.Val = s root = root.Left } else { next := root.Right for next.Left != nil && next.Left != root { next = next.Left } if next.Left == nil { next.Left = root root = root.Right } else { s += root.Val root.Val = s next.Left = nil root = root.Left } } } return node } -
-
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public TreeNode convertBST(TreeNode root) { int s = 0; TreeNode node = root; while (root != null) { if (root.right == null) { s += root.val; root.val = s; root = root.left; } else { TreeNode next = root.right; while (next.left != null && next.left != root) { next = next.left; } if (next.left == null) { next.left = root; root = root.right; } else { s += root.val; root.val = s; next.left = null; root = root.left; } } } return node; } } -
/** * 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* convertBST(TreeNode* root) { int s = 0; TreeNode* node = root; while (root) { if (root->right == nullptr) { s += root->val; root->val = s; root = root->left; } else { TreeNode* next = root->right; while (next->left && next->left != root) { next = next->left; } if (next->left == nullptr) { next->left = root; root = root->right; } else { s += root->val; root->val = s; next->left = nullptr; root = root->left; } } } return node; } }; -
# Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def convertBST(self, root: TreeNode) -> TreeNode: s = 0 node = root while root: if root.right is None: s += root.val root.val = s root = root.left else: next = root.right while next.left and next.left != root: next = next.left if next.left is None: next.left = root root = root.right else: s += root.val root.val = s next.left = None root = root.left return node -
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ func convertBST(root *TreeNode) *TreeNode { s := 0 node := root for root != nil { if root.Right == nil { s += root.Val root.Val = s root = root.Left } else { next := root.Right for next.Left != nil && next.Left != root { next = next.Left } if next.Left == nil { next.Left = root root = root.Right } else { s += root.Val root.Val = s next.Left = nil root = root.Left } } } return node }