
- 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 an arbitrary Binary Tree to a tree that holds Children Sum Property in C++
In this tutorial, we will be discussing a program to convert an arbitrary binary tree to a tree that holds children sum property.
For this we will be provided with a binary tree. Our task is to convert it into the binary tree that follows the children sum property. But the restriction is that we can only increment the values present in the nodes, neither can change the structure of the tree or decrement values in the node.
Example
#include<iostream> #include<bits/stdc++.h> using namespace std; //node structure for binary tree class node{ public: int data; node* left; node* right; //creation of new node node(int data){ this->data = data; this->left = NULL; this->right = NULL; } }; //incrementing left subtree void increment(node* node, int diff); //main function converting the tree void convert_Btree(node* node){ int left_data = 0, right_data = 0, diff; //returning true in case of root //or leaf node if (node == NULL || (node->left == NULL && node->right == NULL)) return; else { //converting left and right subtrees convert_Btree(node->left); convert_Btree(node->right); if (node->left != NULL) left_data = node->left->data; if (node->right != NULL) right_data = node->right->data; //getting children sum diff = left_data + right_data - node->data; //if children sum is greater than node data if (diff > 0) node->data = node->data + diff; if (diff > 0) increment(node, -diff); } } //incrementing the node value void increment(node* node, int diff){ if(node->left != NULL) { node->left->data = node->left->data + diff; //moving recursively in the tree increment(node->left, diff); } else if (node->right != NULL) { node->right->data = node->right->data + diff; increment(node->right, diff); } } //printing inorder traversal void printInorder(node* node){ if (node == NULL) return; printInorder(node->left); cout<<node->data<<" "; printInorder(node->right); } int main(){ node *root = new node(50); root->left = new node(7); root->right = new node(2); root->left->left = new node(3); root->left->right = new node(5); root->right->left = new node(1); root->right->right = new node(30); cout << "Before conversion: " << endl; printInorder(root); convert_Btree(root); cout << "\nAfter conversion: " << endl; printInorder(root); return 0; }
OUTPUT
Before conversion: 3 7 5 50 1 2 30 After conversion: 14 19 5 50 1 31 30
- Related Articles
- Convert a given Binary tree to a tree that holds Logical AND property on C++
- Check for Children Sum Property in a Binary Tree in C++
- Maximum parent children sum in Binary tree in C++
- Binary Search Tree to Greater Sum Tree in C++
- Convert a Binary Tree into its Mirror Tree in C++
- Convert a Binary Tree to Threaded binary tree | Set 1 (Using Queue) in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Convert Ternary Expression to a Binary Tree in C++
- Diagonal Sum of a Binary Tree in C++?
- Binary Tree to Binary Search Tree Conversion in C++
- Maximum Path Sum in a Binary Tree in C++
- Convert Sorted List to Binary Search Tree in C++
- Maximum Level Sum of a Binary Tree in C++
- Maximum spiral sum in Binary Tree in C++

Advertisements