
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Add all greater values to every node in the given BST
Here we will see one interesting problem, where we will add greater values to every node in one given binary search tree. So the initial and final tree will be look like below −
Algorithm
bstUpdate(root, sum) −
Begin if root is null, then stop bstUpdate(right of room, sum) sum := sum + value of root update root value using sum bstUpdate(left of room, sum) End
Example
#include<iostream> using namespace std; class Node { public: int data; Node *left, *right; }; Node *getNode(int item) { Node *newNode = new Node(); newNode->data = item; newNode->left = newNode->right = NULL; return newNode; } void updateBST(Node *root, int *sum) { if (root == NULL) return; updateBST(root->right, sum); //update right sub tree *sum = *sum + root->data; root->data = *sum; //update root data updateBST(root->left, sum); //update left sub tree } void BSTUpdate(Node *root) { int sum = 0; updateBST(root, &sum); } void inorder(Node *root) { if (root != NULL) { inorder(root->left); cout<<root->data<<" "; inorder(root->right); } } Node* insert(Node* node, int data) { if (node == NULL) return getNode(data); if (data <= node->data) //go to left node->left = insert(node->left, data); else //go to right node->right = insert(node->right, data); return node; } int main() { int data[] = {50, 30, 20, 40, 70, 60, 80}; int n = sizeof(data)/sizeof(data[0]); Node *root = NULL; for(int i = 0; i < n; i++) { root = insert(root, data[i]); } BSTUpdate(root); inorder(root); }
Output
350 330 300 260 210 150 80
- Related Articles
- Add all greater values to every node in a given BST?
- Add all greater values to every node in a given BST in C++ ?
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key in C++
- Find all reachable nodes from every node present in a given set in C++
- Convert BST to Greater Tree in C++
- C# Program to add a node after the given node in a Linked List
- C# Program to add a node before the given node in a Linked List
- Delete Node in a BST in C++
- Find all the pairs with given sum in a BST in C++
- Golang Program to add the first node in a given linked list.
- Python program to check if all the values in a list that are greater than a given value
- C# program to check if all the values in a list that are greater than a given value
- Program to check if all the values in a list that are greater than a given value in Python
- Finding next greater node for each node in JavaScript
- Golang Program to add a node at the end of a given linked list.

Advertisements