Construct a Maximum Binary Tree from two given Binary Trees

Given two Binary Trees, the task is to create a Maximum Binary Tree from the two given binary trees and print the Inorder Traversal of that tree.

What is the maximum Binary Tree?

_The __maximum binary __is constructed in the following manner: _

_In the case of both the Binary Trees having two corresponding nodes, the maximum of the two values is considered as the node value of the Maximum Binary Tree. _

_If any of the two nodes is NULL and if the other node is not null, insert that value on that node of the Maximum Binary Tree. _

Example:

Input:
Tree 1                Tree 2
   3                    5 
  / \                  / \
 2   6                1   8 
/                      \   \ 
20                      2   8 
Output: 20 2 2 5 8 8
Explanation:
          5
         / \
        2   8
       / \   \
      20   2   8

To construct the required Binary Tree,
Root Node value: Max(3, 5) = 5
Root->left value: Max(2, 1) = 2
Root->right value: Max(6, 8) = 8
Root->left->left value: 20
Root->left->right value: 2
Root->right->right value: 8

Input:
       Tree 1            Tree 2 
         9                 5
        / \               / \
       2   6             1   8
      / \                 \   \
     20  3                 2   8
Output:  20 2 3 9 8 8
Explanation:
          9
         / \
        2   8
       / \   \
      20  3   8

#data structures #mathematical #recursion #tree #preorder traversal #tree traversals

What is GEEK

Buddha Community

Construct a Maximum Binary Tree from two given Binary Trees

Construct a Maximum Binary Tree from two given Binary Trees

Given two Binary Trees, the task is to create a Maximum Binary Tree from the two given binary trees and print the Inorder Traversal of that tree.

What is the maximum Binary Tree?

_The __maximum binary __is constructed in the following manner: _

_In the case of both the Binary Trees having two corresponding nodes, the maximum of the two values is considered as the node value of the Maximum Binary Tree. _

_If any of the two nodes is NULL and if the other node is not null, insert that value on that node of the Maximum Binary Tree. _

Example:

Input:
Tree 1                Tree 2
   3                    5 
  / \                  / \
 2   6                1   8 
/                      \   \ 
20                      2   8 
Output: 20 2 2 5 8 8
Explanation:
          5
         / \
        2   8
       / \   \
      20   2   8

To construct the required Binary Tree,
Root Node value: Max(3, 5) = 5
Root->left value: Max(2, 1) = 2
Root->right value: Max(6, 8) = 8
Root->left->left value: 20
Root->left->right value: 2
Root->right->right value: 8

Input:
       Tree 1            Tree 2 
         9                 5
        / \               / \
       2   6             1   8
      / \                 \   \
     20  3                 2   8
Output:  20 2 3 9 8 8
Explanation:
          9
         / \
        2   8
       / \   \
      20  3   8

#data structures #mathematical #recursion #tree #preorder traversal #tree traversals

Check if all the Nodes in a Binary Tree having common values are at least D distance apart

Given a Binary Tree and an integer D, the task is to check if the distance between all pairs of same node values in the Tree is ? D or not. If found to be true, then print Yes. Otherwise, print No.

Examples:

Input:_ D = 7 _

                1
              /   \ 
             2     3
            / \   /  \ 
           4   3  4   4

Output:_ Yes _

Explanation:

_The repeated value of nodes are 3 and 4. _

_The distance between the two nodes valued 3, is 3. _

_The maximum distance between any pair of nodes valued 4 is 4. _

Therefore, none of the distances exceed 7

Input:_ D = 1 _

          3
         / \
        3   3
             \
              3

Output:_ No _

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

**Approach: **

The idea is to observe that the problem is similar to finding the distance between two nodes of a tree. But there can be multiple pairs of nodes for which we have to find the distance. Follow the steps below:

  1. Perform the Post Order Traversal of the given tree and find the distance between the repeated pairs of nodes.
  2. Find the nodes that are repeated in the tree using unordered_map.
  3. For each repeated node of a particular value, find the maximum possible distance between any pair.
  4. If that distance is > D, print “No”.
  5. If no such node value is found having a pair containing that value, exceeding **D, **then print “Yes”.

#greedy #recursion #searching #tree #binary tree #frequency-counting #postorder traversal #tree-traversal

How To Merge Two Binary Trees In JavaScript

Problem

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not.

You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example input:

Image for post

#trees #javascript #merge-two-trees #leetcode #depth-first-search

Double Order Traversal of a Binary Tree

Given a Binary Tree consisting of** N** nodes, the task is to print its Double Order Traversal.

Double Order Traversal_ is a tree traversal technique in which every node is traversed twice in the following order: _

  • Visit the Node.
  • Traverse the Left Subtree.
  • Visit the Node.
  • Traverse the Right Subtree.

Examples:

Input:
        1
      /   \
     7     3
    / \   /
   4   5 6
Output: 1 7 4 4 7 5 5 1 3 6 6 3 

Input:
        1
      /   \
     7     3
    / \     \
   4   5     6
Output: 1 7 4 4 7 5 5 1 3 3 6 6

Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:

The idea is to perform Inorder Traversal recursively on the given Binary Tree and print the node value on **visiting a vertex **and after the recursive call to the left subtree during the traversal.

Follow the steps below to solve the problem:

#data structures #recursion #tree #binary tree #inorder traversal #data analysis

Mitchel  Carter

Mitchel Carter

1597791600

Sum of previous numbers that are greater than current number for given array

Given an array A[], for each element in the array, the task is to find the sum of all the previous elements which are strictly greater than the current element.

Examples:

_Input: _A[] = {2, 6, 4, 1, 7}

_Output: _0 0 6 12 0

Explanation:

For 2 and 6 there is no element greater to it on the left.

For 4 there is 6.

For 1 the sum would be 12.

For 7 there is again no element greater to it.

_Input: _A[] = {7, 3, 6, 2, 1}

Output:_ 0 7 7 16 18_

Explanation:

_For 7 there is no element greater to it on the left. _

For 3 there is 7.

For 6 the sum would be 7.

For 2 it has to be 7 + 3 + 6 = 16.

For 1 the sum would be 7 + 3 + 6 + 2 = 18

Naive Approach: For each element, the idea is to find the elements which are strictly greater than the current element on the left side of it and then find the sum of all those elements.

Below is the implementation of the above approach:

  • C++

// C++ program for the above approach

#include <bits/stdc++.h>

**using** **namespace** std;

// Max Element of the Array

**const** **int** maxn = 1000000;

// Function to find the sum of previous

// numbers that are greater than the

// current number for the given array

**void** sumGreater(``**int** ar[], **int** N)

{

// Loop to iterate over all

// the elements of the array

**for** (``**int** i = 0; i < N; i++) {

// Store the answer for

// the current element

**int** cur_sum = 0;

// Iterate from (current index - 1)

// to 0 and check if ar[j] is greater

// than the current element and add

// it to the cur_sum if so

**for** (``**int** j = i - 1; j >= 0; j--) {

**if** (ar[j] > ar[i])

cur_sum += ar[j];

}

// Print the answer for

// current element

cout << cur_sum << " "``;

}

}

// Driver Code

**int** main()

{

// Given array arr[]

**int** ar[] = { 7, 3, 6, 2, 1 };

// Size of the array

**int** N = **sizeof** ar / **sizeof** ar[0];

// Function call

sumGreater(ar, N);

**return** 0;

}

Output:

0 7 7 16 18

_Time Complexity: _O(N2)

_Auxiliary Space: _O(1)

Efficient Approach: To optimize the above approach the idea is to use Fenwick Tree. Below are the steps:

  1. Traverse the given array and find the sum(say total_sum) of all the elements stored in the Fenwick Tree.
  2. Now Consider each element(say arr[i]) as the index of the Fenwick Tree.
  3. Now find the sum of all the elements(say curr_sum) which is smaller than the current element using values stored in Tree.
  4. The value of total_sum – curr_sum will give the sum of all elements which are strictly greater than the elements on the left side of the current element.
  5. Update the current element in the Fenwick Tree.
  6. Repeat the above steps for all the elements in the array.

#arrays #competitive programming #tree #binary indexed tree #bit #segment-tree