Перейти к содержимому

Convert BST to Greater Tree | Greater Sum Tree | Reverse Inorder Traversal | Python, Java & C++

RisingBrain

0:00 / 0:00

Convert BST to Greater Tree | Greater Sum Tree | Reverse Inorder Traversal | Python, Java & C++

907 просмотров · 2 недели назад
RisingBrain
41,6 тыс. подписчиков
907 просмотров · 2 недели назад
In this video, we solve the Convert BST to Greater Tree problem, an important Binary Search Tree problem for coding interviews. The goal is to modify every node so that its value becomes the sum of all values greater than or equal to it in the BST. What you'll learn: How to convert a BST into a Greater Tree Why normal inorder traversal is not enough Using Reverse Inorder Traversal How to maintain a running sum Step-by-step dry run Recursive approach Python, Java & C++ implementation Time and Space Complexity Important BST interview concepts Key Idea For a BST, normal inorder traversal gives: Left → Root → Right which produces values in increasing order. To process greater values first, we use Reverse Inorder Traversal: Right → Root → Left We maintain a running sum and update each node with: node.val = node.val + sum This converts the BST into a Greater Tree / Greater Sum Tree. Complexity Time: O(N) Space: O(H) where N is the number of nodes and H is the height of the BST. This is an important BST + Reverse Inorder + Recursion pattern and is useful for coding interview preparation. Rising Brain (DSA Sheet): https://www.risingbrain.org/sheet Connect with me here: LinkedIn –   / anjalikumari22   Instagram –   / rbanjali.codes   Twitter (X) – https://x.com/anjali1kumari?s=21