Open In App

Maximum number in Binary tree of binary values

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:

Below is the implementation for the above approach:




// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
 
// Bulid tree class
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;
    }
 
    // Update the currentNumber by shifting
    // left and adding the node's value
    currentNumber = (currentNumber << 1) | root->data;
 
    // Recursively find the maximum number
    // in the left and right subtrees
    int leftMax
        = findMaxBinaryNumber(root->left, currentNumber);
    int rightMax
        = findMaxBinaryNumber(root->right, currentNumber);
 
    // Return the maximum of the current subtree
    return max(currentNumber, max(leftMax, rightMax));
}
 
// Driver Code
int main()
{
 
    // Create a binary tree
    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);
 
    // Find the maximum binary number in the tree
    int maxNumber = findMaxBinaryNumber(root, 0);
    cout << "Maximum Binary Number: " << maxNumber;
 
    return 0;
}




// Java implementation of the above approach
import java.util.*;
 
// Build tree class
class Node {
    int data;
    Node left, right;
 
    public Node(int val)
    {
        data = val;
        left = right = null;
    }
}
 
public class GFG {
    // Function to find the maximum binary number
    static int findMaxBinaryNumber(Node root,
                                   int currentNumber)
    {
        if (root == null) {
            return 0;
        }
 
        // Update the currentNumber by shifting
        // left and adding the node's value
        currentNumber = (currentNumber << 1) | root.data;
 
        // Recursively find the maximum number
        // in the left and right subtrees
        int leftMax
            = findMaxBinaryNumber(root.left, currentNumber);
        int rightMax = findMaxBinaryNumber(root.right,
                                           currentNumber);
 
        // Return the maximum of the current subtree
        return Math.max(currentNumber,
                        Math.max(leftMax, rightMax));
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Create a binary tree
        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);
 
        // Find the maximum binary number in the tree
        int maxNumber = findMaxBinaryNumber(root, 0);
        System.out.println("Maximum Binary Number: "
                           + maxNumber);
    }
}
 
// This code is contributed by Susobhan Akhuli




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
 
    # Update the current_number by shifting left and adding the node's value
    current_number = (current_number << 1) | root.data
 
    # Recursively find the maximum number in the left and right subtrees
    left_max = find_max_binary_number(root.left, current_number)
    right_max = find_max_binary_number(root.right, current_number)
 
    # Return the maximum of the current subtree
    return max(current_number, max(left_max, right_max))
 
# Driver Code
if __name__ == "__main__":
    # Create a binary tree
    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)
 
    # Find the maximum binary number in the tree
    max_number = find_max_binary_number(root, 0)
    print("Maximum Binary Number:", max_number)
 
# This code is cotributed by akshitaguprzj3




using System;
 
// Build tree class
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;
        }
 
        // Update the currentNumber by shifting
        // left and adding the node's value
        currentNumber = (currentNumber << 1) | root.data;
 
        // Recursively find the maximum number
        // in the left and right subtrees
        int leftMax = FindMaxBinaryNumber(root.left, currentNumber);
        int rightMax = FindMaxBinaryNumber(root.right, currentNumber);
 
        // Return the maximum of the current subtree
        return Math.Max(currentNumber, Math.Max(leftMax, rightMax));
    }
 
    // Driver Code
    static void Main()
    {
        // Create a binary tree
        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);
 
        // Find the maximum binary number in the tree
        int maxNumber = FindMaxBinaryNumber(root, 0);
        Console.WriteLine("Maximum Binary Number: " + maxNumber);
    }
}




// JavaScript implementation of above approach
 
// Build tree class
class Node {
    constructor(val) {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}
 
function findMaxBinaryNumber(root, currentNumber) {
    if (root === null) {
        return 0;
    }
 
    // Update the currentNumber by shifting
    // left and adding the node's value
    currentNumber = (currentNumber << 1) | root.data;
 
    // Recursively find the maximum number
    // in the left and right subtrees
    let leftMax = findMaxBinaryNumber(root.left, currentNumber);
    let rightMax = findMaxBinaryNumber(root.right, currentNumber);
 
    // Return the maximum of the current subtree
    return Math.max(currentNumber, Math.max(leftMax, rightMax));
}
 
// Create a binary tree
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);
 
// Find the maximum binary number in the tree
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.


Article Tags :