Q:
Given a Binary Tree where each node has positive and negative values. Convert this to a tree where each node contains the sum of the left and right sub trees in the original tree. The values of leaf nodes are changed to 0
belongs to collection: Interview C++ coding problems/challenges | tree
Interview C++ coding problems/challenges | tree
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Given a Binary Tree T and a sum S, write a program to check whether there is a root to leaf path in that tree with the input sum S
- Given a Binary Tree write a program to print the nodes which don’t have a sibling node. Print all the nodes separated by space which do not have sibling in the tree in sorted order if every node has a tree than print -1
- Given a Two Binary Trees, write a function that returns true if one is mirror of other, else returns false
- Given a Binary Tree where each node has positive and negative values. Convert this to a tree where each node contains the sum of the left and right sub trees in the original tree. The values of leaf nodes are changed to 0
- Given a binary Tree, check whether the tree is symmetric or not
- Write a program to print Reverse Level Order Traversal of a binary tree
- Given an array where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1
- Given a Binary Tree, write a function getLevelDiff which returns the difference between the sum of nodes at odd level and the sum of nodes at even level
- Write a function to detect if two trees are isomorphic
- Given an expression tree evaluate the expression tree
- Given a Binary Tree and a number K. Print all nodes that are at distance K from root (root is considered at distance 0 from itself)
- Given a Binary Tree, print Right view of it. Right view of a Binary Tree is set of nodes visible when tree is visited from Right side
- Given a Binary Tree, find diameter of it. The diameter of a tree is the number of nodes on the longest path between two leaves in the tree
- Given a BST and a value x, write a function to delete the nodes having values greater than or equal to x. The function will return the modified root
- Given a binary tree, print the diagonal traversal of the binary tree
- Given a Binary Tree, Print the corner nodes at each level. The node at the leftmost and the node at the rightmost
- Given a Binary Search Tree and 2 nodes value n1 and n2, your task is to find the lowest common ancestor of the two nodes. Assume that n1 and n2 both existing node value of the tree
- Given a string that contains ternary expressions. The expressions may be nested. You need to convert the given ternary expression to a binary Tree and return the root
- Given a binary tree, print the bottom view from left to right
- Given a Binary Tree and a target key, write a function that prints all the ancestors of the key in the given binary tree
- Given a Binary Tree of size N, write a program that prints all the possible paths from root node to the all the leaf node\'s of the binary tree
- Given a binary tree, where every node value is a number between 0-9. Find the sum of all the numbers which are formed from root to leaf paths
- Given a binary tree, and two node values your task is to find the minimum distance between them
- Find the k-th smallest element in a given binary search tree (BST)
- Write a program to print Level Order Traversal in spiral form of a binary tree
- Given a binary Tree find the maximum path sum. The path may start and end at any node in the tree
- Given an array pre[] of N nodes representing preorder traversal of BST. The task is to print its postorder traversal
- Given two n-ary trees, the task is to check if they are mirrors of each other or not
- Find number of nodes in a complete Binary Tree

C++ programming
Sum tree
A binary tree is said to be converted in sum tree:
Let’s consider,
For any intermediate node having two child at kth level
Value of the node must be updated as
Sum of right subtree of the node+ sum of left subtree of the node
Now consider both the right & left child has been already converted to their update node values having respective subtree sums up to (k+1)th level (from bottom to (k+1)th level).
So the node value should be updated as: right child value + left child value + subtree sums up to (k+1)th level
=old right child value + old left child value + new left child value (sum of subtrees up to (k+1)th level) + new right child value (sum of subtrees up to (k+1)th level).
So, here we can develop our recursive function to develop the algo
FUNCTION toSumTree (node) 1. Check base case IF (node==NULL) Return 0; 2. Store old value of node 3. Update node value to left subtree sum + right subtree sum. Use recursion to find subtree sums 4. Return old node value + current node value for upper levels ( towards root) END FUNCTIONC++ implementation for Transform to Sum Tree
Output
Explanation with example:
Let’s see how the function solves the above example.
Original tree: 10 / \ -2 8 / \ / \ 6 -5 -7 5 In the main function it calls toSumTree (10) ------------------------------------------------------------ toSumTree (10) //call at Main() node is not NULL temp=10 new node value = toSumTree (10->left) + toSumTree (10->right) =toSumTree (-2) + toSumTree (8) Thus call to toSumTree (-2) and toSumTree (8) Return new node value + temp (10) ------------------------------------------------------------ toSumTree (-2) //call at toSumTree (10) node is not NULL temp=-2 new node value = toSumTree (-2->left) + toSumTree (-2->right) =toSumTree (6) + toSumTree (-5) Thus call to toSumTree (6) and toSumTree (-5) Return new node value + temp (-2) ------------------------------------------------------------ toSumTree (8) //call at toSumTree (10) node is not NULL temp=8 new node value = toSumTree (8->left) + toSumTree (8->right) =toSumTree (-7) + toSumTree (5) Thus call to toSumTree (-7) and toSumTree (5) Return new node value + temp (8) ------------------------------------------------------------ toSumTree (6) //call at toSumTree (-2) node is not NULL temp=6 new node value = toSumTree (6->left) + toSumTree (6->right) =toSumTree (NULL) + toSumTree (NULL) Thus call to toSumTree (NULL) and toSumTree (NULL) Return new node value + temp (6) ------------------------------------------------------------ toSumTree (-5) //call at toSumTree (-2) node is not NULL temp=-5 new node value = toSumTree (-5->left) + toSumTree (-5->right) =toSumTree (NULL) + toSumTree (NULL) Thus call to toSumTree (NULL) and toSumTree (NULL) Return new node value + temp (-5) ------------------------------------------------------------ toSumTree (-7)//call at toSumTree(8) node is not NULL temp=-7 new node value = toSumTree (-7->left) + toSumTree (-7->right) =toSumTree (NULL) + toSumTree (NULL) Thus call to toSumTree (NULL) and toSumTree (NULL) Return new node value + temp (-7) ------------------------------------------------------------ toSumTree (5)//call at toSumTree (8) node is not NULL temp=5 new node value = toSumTree (5->left) + toSumTree (5->right) =toSumTree (NULL) + toSumTree (NULL) Thus call to toSumTree (NULL) and toSumTree (NULL) Return new node value + temp (5) ------------------------------------------------------------ toSumTree (NULL)//call at toSumTree(6) node is NULL return 0 ------------------------------------------------------------ toSumTree (NULL)//call at toSumTree (6) node is NULL return 0 ------------------------------------------------------------ toSumTree (NULL)//call at toSumTree(-5) node is NULL return 0 ------------------------------------------------------------ toSumTree (NULL)//call at toSumTree(-5) node is NULL return 0 ------------------------------------------------------------ toSumTree (NULL)//call at toSumTree(-7) node is NULL return 0 ------------------------------------------------------------ toSumTree (NULL)//call at toSumTree(-7) node is NULL return 0 ------------------------------------------------------------ toSumTree (NULL)//call at toSumTree(5) node is NULL return 0 ------------------------------------------------------------ toSumTree (NULL)//call at toSumTree(5) node is NULL return 0 At toSumTree (6) //call at toSumTree(-2) new node value = toSumTree (6->left) + toSumTree (6->right) =toSumTree (NULL) + toSumTree (NULL) =0 6->0 It returns 0+6=6 ------------------------------------------------------------ At toSumTree (-5) //call at toSumTree(-2) new node value = toSumTree (-5->left) + toSumTree (-5->right) =toSumTree (NULL) + toSumTree (NULL) =0 -5->0 It returns 0-5=-5 ------------------------------------------------------------ At toSumTree (-7) //call at toSumTree(8) new node value = toSumTree (-7->left) + toSumTree (-7->right) =toSumTree (NULL) + toSumTree (NULL) =0 -7->0 It returns 0-7=-7 ------------------------------------------------------------ At toSumTree (5) //call at toSumTree (8) new node value = toSumTree (5->left) + toSumTree (5->right) =toSumTree (NULL) + toSumTree (NULL) =0 5->0 It returns 0+5=5 ------------------------------------------------------------ At toSumTree (-2) //call at toSumTree(10) new node value = toSumTree (-2->left) + toSumTree (-2->right) =toSumTree (6) + toSumTree (-5) =6 + (-5) =1 -2->1 It returns -2+1=-1 ------------------------------------------------------------ At toSumTree (8) //call at toSumTree (10) new node value = toSumTree (-8->left) + toSumTree (-8->right) =toSumTree (-7) + toSumTree (5) =-7 + (5) =-2 8->-2 It returns -2+8=6 ------------------------------------------------------------ At toSumTree (10) //call at main new node value = toSumTree (10->left) + toSumTree (10->right) =toSumTree (-2) + toSumTree (8) =-1 + 6=5 10->5 It returns 10+5=15 So, the binary tree is sum converted to: 5 / \ 1 -2 / \ / \ 0 0 0 0need an explanation for this answer? contact us directly to get an explanation for this answer