
- 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
Diagonal Sum of a Binary Tree in C++?
To consider the nodes that are passing between lines of slope -1. The diagonal sum of the binary tree will be calculated by the sum of all nodes data that are present between these lines of reference.
Let us first define the struct that would represent a tree node that contains the data and its left and right node child. If this is the first node to be created then it’s a root node otherwise a child node.
struct Node { int data; struct Node *leftChild, *rightChild; };
Next we create our createNode(int data) function that takes an int value and assign it to the data member of the node after creating the new node. The function returns the pointer to the created node.
Node * createNode(int data){ Node * node = new Node; node->data = data; return node; }
Next, the diagonal_sum(Node *root, int depth, map<int, int> &diagonalSum) takes the root node, current depth and the diagonalSum map by reference. If root isn’t NULL then we add the current root data to the current depth index in the diagonalSum map to get the sum of elements .We then perform an recursive inorder traversal on the tree and add 1 to the depth whenever we move to the left child.
void diagonal_sum(Node *root, int depth, map<int, int> &diagonalSum){ if(root){ diagonalSum[depth]+=root->data; diagonal_sum(root->leftChild, depth+1, diagonalSum); diagonal_sum(root->rightChild, depth, diagonalSum); } }
Inside our main function we create a tree using the createNode(data) method and also create a map SumMap. The root node , current depth which is 1 and the sumMap are sent to diagonal_sum where the sumMap is filled with key-values pair. Next we create our iterator it for iterating over the sumMap map.
int main(){ Node *root = createNode(1); root->rightChild = createNode(3); root->rightChild->leftChild = createNode(4); root->rightChild->leftChild->leftChild = createNode(12); root->rightChild->leftChild->rightChild = createNode(7); root->leftChild = createNode(2); root->leftChild->leftChild = createNode(9); root->leftChild->rightChild = createNode(6); root->leftChild->leftChild->rightChild = createNode(10); root->leftChild->rightChild->leftChild = createNode(11); root->rightChild->rightChild = createNode(5); map<int,int> sumMap; diagonal_sum(root, 1, sumMap); map<int,int>::iterator it;
Finally we iterate on our SumMap by using the iterator it inside our for loop and print the diagonal sums.
for(it=sumMap.begin(); it!=sumMap.end();++it){ int value = it->second; cout<<value<<"\t"; }
Example
Let us see the following implementation for finding the diagonal sum of a binary tree.
#include<iostream> #include<map> using namespace std; struct Node{ int data; struct Node* leftChild, *rightChild; }; Node * createNode(int data){ Node * node = new Node; node->data = data; return node; } void diagonal_sum(Node *root, int depth, map<int, int> &diagonalSum){ if(root){ diagonalSum[depth]+=root->data; diagonal_sum(root->leftChild, depth+1, diagonalSum); diagonal_sum(root->rightChild, depth, diagonalSum); } } int main(){ Node *root = createNode(1); root->rightChild = createNode(3); root->rightChild->leftChild = createNode(4); root->rightChild->leftChild->leftChild = createNode(12); root->rightChild->leftChild->rightChild = createNode(7); root->leftChild = createNode(2); root->leftChild->leftChild = createNode(9); root->leftChild->rightChild = createNode(6); root->leftChild->leftChild->rightChild = createNode(10); root->leftChild->rightChild->leftChild = createNode(11); root->rightChild->rightChild = createNode(5); map<int,int> sumMap; diagonal_sum(root, 1, sumMap); map<int,int>::iterator it; for(it=sumMap.begin(); it!=sumMap.end();++it){ int value = it->second; cout<<value<<"\t"; } return 0; }
Output
The above code will produce the following output −
91942
- Related Articles
- Diagonal Traversal of Binary Tree in C++?
- Kth node in Diagonal Traversal of Binary Tree in C++
- Maximum Level Sum of a Binary Tree in C++
- Program to find sum each of the diagonal path elements in a binary tree in Python
- Binary Search Tree to Greater Sum Tree in C++
- Maximum Path Sum in a Binary Tree in C++
- Maximum spiral sum in Binary Tree in C++
- Maximum Sum BST in Binary Tree in C++
- Find maximum vertical sum in binary tree in C++
- Maximum parent children sum in Binary tree in C++
- Find maximum level sum in Binary Tree in C++
- Check for Children Sum Property in a Binary Tree in C++
- Print all k-sum paths in a binary tree in C++
- Find sum of all left leaves in a given Binary Tree in C++
- Find sum of all right leaves in a given Binary Tree in C++
