Maximum number in Binary tree of binary values
Last Updated :
18 Jan, 2024
Given a binary tree consisting of nodes, each containing a binary value of either 0 or 1, the task is to find the maximum decimal number that can be formed by traversing from the root to a leaf node. The maximum number is achieved by concatenating the binary values along the path from the root to a leaf.
Examples:
Input:
1
/ \
0 1
/ \ / \
0 1 1 0
Output: 7
Explaination: The possible binary numbers are 100, 101, 111, 110, So the maximum number in decimal is 7 (111).
Input:
1
/ \
1 0
/ \ \
0 1 0
/
1
Output: 15
Explaination: The possible binary numbers are 110, 1111, 100. So the maximum number in decimal is 15 (1111).
Approach: To solve the problem follow the below Intuition:
The algorithm in binary tree is a DFS recursive traversal, accumulating the binary number as it traverses the tree from the root to the leaves. At each node, the binary number is updated by shifting it left (equivalent to multiplying by 2) and adding the binary value of the current node. The algorithm considers both left and right subtrees, comparing their maximum binary numbers. By the end of the traversal, the algorithm returns the maximum binary number that can be formed from the root to any leaf in the binary tree.
Follow the steps to solve the problem:
- Declare a currentNumber, which is the binary number formed from the root node to the current node.
- Travese recusively and if the current node is NULL (i.e., a leaf node is reached, thus base case hits), then return 0 because there is no number to add to the currentNumber.
- The currentNumber is updated by shifting it left (equivalent to multiplying by 2) and adding the binary value of the current node (0 or 1) using bitwise OR operation.
- The function then recursively calls itself for the left and right children, passing the updated currentNumber. It calculates the maximum binary number for the left and right subtrees.
- Finally, returns the maximum of the currentNumber and the maximum binary numbers obtained from the left and right subtrees.
Below is the implementation for the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* left;
Node* right;
Node( int val)
{
data = val;
left = NULL;
right = NULL;
}
};
int findMaxBinaryNumber(Node* root, int currentNumber)
{
if (root == NULL) {
return 0;
}
currentNumber = (currentNumber << 1) | root->data;
int leftMax
= findMaxBinaryNumber(root->left, currentNumber);
int rightMax
= findMaxBinaryNumber(root->right, currentNumber);
return max(currentNumber, max(leftMax, rightMax));
}
int main()
{
Node* root = new Node(1);
root->left = new Node(0);
root->right = new Node(1);
root->left->left = new Node(0);
root->left->right = new Node(1);
root->right->left = new Node(1);
root->right->right = new Node(0);
int maxNumber = findMaxBinaryNumber(root, 0);
cout << "Maximum Binary Number: " << maxNumber;
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node left, right;
public Node( int val)
{
data = val;
left = right = null ;
}
}
public class GFG {
static int findMaxBinaryNumber(Node root,
int currentNumber)
{
if (root == null ) {
return 0 ;
}
currentNumber = (currentNumber << 1 ) | root.data;
int leftMax
= findMaxBinaryNumber(root.left, currentNumber);
int rightMax = findMaxBinaryNumber(root.right,
currentNumber);
return Math.max(currentNumber,
Math.max(leftMax, rightMax));
}
public static void main(String[] args)
{
Node root = new Node( 1 );
root.left = new Node( 0 );
root.right = new Node( 1 );
root.left.left = new Node( 0 );
root.left.right = new Node( 1 );
root.right.left = new Node( 1 );
root.right.right = new Node( 0 );
int maxNumber = findMaxBinaryNumber(root, 0 );
System.out.println( "Maximum Binary Number: "
+ maxNumber);
}
}
|
Python3
class Node:
def __init__( self , val):
self .data = val
self .left = None
self .right = None
def find_max_binary_number(root, current_number):
if not root:
return 0
current_number = (current_number << 1 ) | root.data
left_max = find_max_binary_number(root.left, current_number)
right_max = find_max_binary_number(root.right, current_number)
return max (current_number, max (left_max, right_max))
if __name__ = = "__main__" :
root = Node( 1 )
root.left = Node( 0 )
root.right = Node( 1 )
root.left.left = Node( 0 )
root.left.right = Node( 1 )
root.right.left = Node( 1 )
root.right.right = Node( 0 )
max_number = find_max_binary_number(root, 0 )
print ( "Maximum Binary Number:" , max_number)
|
C#
using System;
class Node
{
public int data;
public Node left;
public Node right;
public Node( int val)
{
data = val;
left = null ;
right = null ;
}
}
class GFG
{
static int FindMaxBinaryNumber(Node root, int currentNumber)
{
if (root == null )
{
return 0;
}
currentNumber = (currentNumber << 1) | root.data;
int leftMax = FindMaxBinaryNumber(root.left, currentNumber);
int rightMax = FindMaxBinaryNumber(root.right, currentNumber);
return Math.Max(currentNumber, Math.Max(leftMax, rightMax));
}
static void Main()
{
Node root = new Node(1);
root.left = new Node(0);
root.right = new Node(1);
root.left.left = new Node(0);
root.left.right = new Node(1);
root.right.left = new Node(1);
root.right.right = new Node(0);
int maxNumber = FindMaxBinaryNumber(root, 0);
Console.WriteLine( "Maximum Binary Number: " + maxNumber);
}
}
|
Javascript
class Node {
constructor(val) {
this .data = val;
this .left = null ;
this .right = null ;
}
}
function findMaxBinaryNumber(root, currentNumber) {
if (root === null ) {
return 0;
}
currentNumber = (currentNumber << 1) | root.data;
let leftMax = findMaxBinaryNumber(root.left, currentNumber);
let rightMax = findMaxBinaryNumber(root.right, currentNumber);
return Math.max(currentNumber, Math.max(leftMax, rightMax));
}
let root = new Node(1);
root.left = new Node(0);
root.right = new Node(1);
root.left.left = new Node(0);
root.left.right = new Node(1);
root.right.left = new Node(1);
root.right.right = new Node(0);
let maxNumber = findMaxBinaryNumber(root, 0);
console.log( "Maximum Binary Number: " + maxNumber);
|
Output
Maximum Binary Number: 7
Time Complexity: O(N)
Auxiliary Space: O(H), where H is height of binary tree.
Share your thoughts in the comments
Please Login to comment...