
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree in C++
In this tutorial, we will be discussing a program to convert a binary tree such that every node stores the sum of all nodes in its right subtree.
For this, we will be provided with a binary tree. Our task is to return another tree where every node must be equal to the sum of the node and its right subtree.
Example
#include <bits/stdc++.h> using namespace std; //node structure of tree struct Node { int data; Node *left, *right; }; //creation of a new node struct Node* createNode(int item){ Node* temp = new Node; temp->data = item; temp->left = NULL; temp->right = NULL; return temp; } //creating the new binary tree int rightsum_tree(Node* root){ if (!root) return 0; if (root->left == NULL && root->right == NULL) return root->data; //changing the values of left/right subtree int rightsum = rightsum_tree(root->right); int leftsum = rightsum_tree(root->left); //adding the sum of right subtree root->data += rightsum; return root->data + leftsum; } //traversing tree in inorder pattern void inorder(struct Node* node){ if (node == NULL) return; inorder(node->left); cout << node->data << " "; inorder(node->right); } int main(){ struct Node* root = NULL; root = createNode(1); root->left = createNode(2); root->right = createNode(3); root->left->left = createNode(4); root->left->right = createNode(5); root->right->right = createNode(6); rightsum_tree(root); cout << "Updated Binary Tree :\n"; inorder(root); return 0; }
Output
Updated Binary Tree : 4 7 5 10 9 6
- Related Articles
- Number of leaf nodes in the subtree of every node of an n-ary tree in C++
- Convert a BST to a Binary Tree such that sum of all greater keys is added to every key in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent in C++
- Python Program to Find the Sum of All Nodes in a Binary Tree
- Find maximum among all right nodes in Binary Tree in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent | Dynamic Programming In C++
- Find sum of all nodes of the given perfect binary tree in C++
- Print all leaf nodes of a binary tree from right to left in C++
- Product of all nodes in a Binary Tree in C++
- Find sum of all right leaves in a given Binary Tree in C++
- Maximum sum of nodes in Binary tree such that no two are adjacent using Dynamic Programming in C++ program
- Print all internal nodes of a Binary tree in C++
- Program to find most frequent subtree sum of a binary tree in Python
- Check if a binary tree is subtree of another binary tree in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++

Advertisements