Given an integer N, the task is to generate a perfect binary tree with height N such that each node has a value that is the same as its depth. Return the inorder traversal of the generated binary tree.
A Perfect binary tree is a type of binary tree where every internal node has exactly two child nodes and all leaf nodes are at same level.Â
Examples:
Input: N = 2
Output: 2 1 2 0 2 1 2
Explanation: The structure of the tree is as following![]()
Perfect Binary Tree with height 2
Input: N = 1
Output: 1 0 1
Approach: The problem can be solved using level order traversal based on the following observation:
As there is a requirement to fill each node with the value that is the same as its depth so use the level order traversal. In each step fill a total level and mark all the nodes with the value same as the depth.
Follow the steps mentioned below to implement the approach:
- Initiate a variable to store the depth of the current level.
- Initiate a queue to store the node of each level and perform the level order traversal.
- In each iteration get all the nodes in that level and continue the following until the depth is N:
- Increase the depth by 1.
- Pop the nodes at the current level.
- Generate the child nodes for each node.
- Add the new child nodes for further processing.
- After the traversal is complete, the tree is prepared.
- Print the inorder traversal of the tree.
Below is the implementation of the above approach.
C++
//C++ code to implement the approachÂ
#include <iostream>#include <queue>using namespace std;Â
// Class to hold tree node data// and left, right childrenclass TreeNode {public:Â Â Â Â long val;Â Â Â Â TreeNode* left;Â Â Â Â TreeNode* right;Â
    TreeNode(long x) {        val = x;        left = NULL;        right = NULL;    }};Â
// Class to create the perfect binary treeclass BinaryTree {Â
    // Method to construct a binary treepublic:    static TreeNode* perfectBinaryTree(int depth) {        // Return root with 0 as val if height is 0        if (depth == 0)            return new TreeNode(0);Â
        // Initiate queue to store        // the nodes on each level        queue<TreeNode*> queue;Â
        // Create a root node with value 0        long i = 0;        TreeNode* root = new TreeNode(i);Â
        // Add the root node to the queue        // for further processing        queue.push(root);Â
        // Iterate through the queue till its empty        while (!queue.empty()) {Â
            // Check the size of the queue to iterate            // through each node on the same level            int size = queue.size();Â
            // Increment the value of node            // upon each level            i++;Â
            // Break the loop if the value of the node            // reaches given depth            // else process the node in the queue            if (i > depth) {                break;            }            else {                // Add the left and right child                // for the node in the queue and                // add those newly created child nodes                // to the queue.                for (int j = 0; j < size; j++) {                    TreeNode* node = queue.front();                    queue.pop();                    node->left = new TreeNode(i);                    node->right = new TreeNode(i);Â
                    queue.push(node->left);                    queue.push(node->right);                }            }        }Â
        // Return the root of the tree        return root;    }Â
    // Inorder traversal of the tree (Left Root Right)public:    static void inOrderTraversal(TreeNode* node) {        if (node == NULL)            return;        inOrderTraversal(node->left);        cout << node->val << " ";        inOrderTraversal(node->right);    }};Â
// Driver codeint main() {Â Â Â Â int N = 2;Â
    // Function call to build the tree    TreeNode* binaryTreeRoot        = BinaryTree::perfectBinaryTree(N);Â
    // Function call to print the tree    BinaryTree::inOrderTraversal(binaryTreeRoot);Â
    return 0;} |
Java
// Java code to implement the approachÂ
import java.util.*;Â
// Class to hold tree node data// and left, right childrenclass TreeNode {Â Â Â Â public long val;Â Â Â Â public TreeNode left;Â Â Â Â public TreeNode right;Â
    public TreeNode(long x)    {        val = x;        left = null;        right = null;    }}Â
// Class to create the perfect binary treeclass BinaryTree {Â
    // Method to construct a binary tree    public static TreeNode perfectBinaryTree(int depth)    {        // Return root with 0 as val if height is 0        if (depth == 0)            return new TreeNode(0);Â
        // Initiate queue to store        // the nodes on each level        Queue<TreeNode> queue            = new LinkedList<>();Â
        // Create a root node with value 0        long i = 0;        TreeNode root = new TreeNode(i);Â
        // Add the root node to the queue        // for further processing        queue.add(root);Â
        // Iterate through the queue till its empty        while (!queue.isEmpty()) {Â
            // Check the size of the queue to iterate            // through each node on the same level            int size = queue.size();Â
            // Increment the value of node            // upon each level            i++;Â
            // Break the loop if the value of the node            // reaches given depth            // else process the node in the queue            if (i > depth) {                break;            }            else {                // Add the left and right child                // for the node in the queue and                // add those newly created child nodes                // to the queue.                for (int j = 0; j < size; j++) {                    TreeNode node = queue.remove();                    node.left = new TreeNode(i);                    node.right = new TreeNode(i);Â
                    queue.add(node.left);                    queue.add(node.right);                }            }        }Â
        // Return the root of the tree        return root;    }Â
    // Inorder traversal of the tree (Left Root Right)    public static void inOrderTraversal(TreeNode node)    {        if (node == null)            return;        inOrderTraversal(node.left);        System.out.print(node.val + " ");        inOrderTraversal(node.right);    }Â
    // Driver code    public static void main(String[] args)    {        int N = 2;Â
        // Function call to build the tree        TreeNode binaryTreeRoot            = perfectBinaryTree(N);Â
        // Function call to print the tree        inOrderTraversal(binaryTreeRoot);    }} |
Python3
# Python code to implement the approach Â
# Class to hold tree node data# and left, right childrenclass TreeNode:    def __init__(self, x):        self.val = x        self.left = None        self.right = NoneÂ
# Class to create the perfect binary treeclass BinaryTree:    # Method to construct a binary tree    def perfectBinaryTree(self, depth):        # Return root with 0 as val if height is 0        if depth == 0:            return TreeNode(0)Â
        # Initiate queue to store        # the nodes on each level        queue = []Â
        # Create a root node with value 0        i = 0        root = TreeNode(i)Â
        # Add the root node to the queue        # for further processing        queue.append(root)Â
        # Iterate through the queue till its empty        while len(queue) > 0:            # Check the size of the queue to iterate            # through each node on the same level            size = len(queue)Â
            # Increment the value of node            # upon each level            i += 1Â
            # Break the loop if the value of the node            # reaches given depth            # else process the node in the queue            if i > depth:                break            else:                # Add the left and right child                # for the node in the queue and                # add those newly created child nodes                # to the queue.                for j in range(size):                    node = queue.pop(0)                    node.left = TreeNode(i)                    node.right = TreeNode(i)Â
                    queue.append(node.left)                    queue.append(node.right)Â
        # Return the root of the tree        return rootÂ
    # Inorder traversal of the tree (Left Root Right)    def inOrderTraversal(self, node):        if node is None:            return        self.inOrderTraversal(node.left)        print(node.val, end=" ")        self.inOrderTraversal(node.right)Â
    # Driver code    def main(self):        N = 2Â
        # Function call to build the tree        binaryTreeRoot = self.perfectBinaryTree(N)Â
        # Function call to print the tree        self.inOrderTraversal(binaryTreeRoot)Â
# Create an object of the BinaryTree classbTree = BinaryTree()Â
# Call the main functionbTree.main() |
C#
// C# code to implement the approachusing System;using System.Collections.Generic;Â
// Class to hold tree node data// and left, right childrenpublic class TreeNode {Â Â public long val;Â Â public TreeNode left;Â Â public TreeNode right;Â
  public TreeNode(long x) {    val = x;    left = null;    right = null;  }}Â
// Class to create the perfect binary treepublic class BinaryTree {Â
  // Method to construct a binary tree  public static TreeNode perfectBinaryTree(int depth) {    // Return root with 0 as val if height is 0    if (depth == 0)      return new TreeNode(0);Â
    // Initiate queue to store    // the nodes on each level    Queue<TreeNode> queue = new Queue<TreeNode>();Â
    // Create a root node with value 0    long i = 0;    TreeNode root = new TreeNode(i);Â
    // Add the root node to the queue    // for further processing    queue.Enqueue(root);Â
    // Iterate through the queue till its empty    while (queue.Count!=0) {Â
      // Check the size of the queue to iterate      // through each node on the same level      int size = queue.Count;Â
      // Increment the value of node      // upon each level      i++;Â
      // Break the loop if the value of the node      // reaches given depth      // else process the node in the queue      if (i > depth) {        break;      } else {        // Add the left and right child        // for the node in the queue and        // add those newly created child nodes        // to the queue.        for (int j = 0; j < size; j++) {          TreeNode node = queue.Dequeue();          node.left = new TreeNode(i);          node.right = new TreeNode(i);Â
          queue.Enqueue(node.left);          queue.Enqueue(node.right);        }      }    }Â
    // Return the root of the tree    return root;  }Â
  // Inorder traversal of the tree (Left Root Right)  public static void inOrderTraversal(TreeNode node) {    if (node == null)      return;    inOrderTraversal(node.left);    Console.Write(node.val + " ");    inOrderTraversal(node.right);  }Â
  // Driver code  public static void Main(String[] args) {    int N = 2;Â
    // Function call to build the tree    TreeNode binaryTreeRoot = perfectBinaryTree(N);Â
    // Function call to print the tree    inOrderTraversal(binaryTreeRoot);  }}Â
// This code is contributed by shikhasingrajput |
Javascript
        // JavaScript code for the above approach        class TreeNode {    constructor(val) {        this.val = val;        this.left = null;        this.right = null;    }}Â
class BinaryTree {Â Â Â Â static perfectBinaryTree(depth) {Â Â Â Â Â Â Â Â if (depth === 0) {Â Â Â Â Â Â Â Â Â Â Â Â return new TreeNode(0);Â Â Â Â Â Â Â Â }Â
        const queue = [];        let i = 0;        const root = new TreeNode(i);        queue.push(root);Â
        while (queue.length > 0) {            const size = queue.length;            i++;            if (i > depth) {                break;            }            else {                for (let j = 0; j < size; j++) {                    const node = queue.shift();                    node.left = new TreeNode(i);                    node.right = new TreeNode(i);                    queue.push(node.left);                    queue.push(node.right);                }            }        }Â
        return root;    }Â
    static inOrderTraversal(node) {        if (node === null) return;        this.inOrderTraversal(node.left);        console.log(node.val + " ");        this.inOrderTraversal(node.right);    }}Â
const N = 2;const binaryTreeRoot = BinaryTree.perfectBinaryTree(N);BinaryTree.inOrderTraversal(binaryTreeRoot);Â
 // This code is contributed by Potta Lokesh. |
2 1 2 0 2 1 2
Time Complexity: O(2N)
Auxiliary Space: O(2N)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!
