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++
#include <iostream>
#include <queue>
using namespace std;
class TreeNode {
public :
long val;
TreeNode* left;
TreeNode* right;
TreeNode( long x) {
val = x;
left = NULL;
right = NULL;
}
};
class BinaryTree {
public :
static TreeNode* perfectBinaryTree( int depth) {
if (depth == 0)
return new TreeNode(0);
queue<TreeNode*> queue;
long i = 0;
TreeNode* root = new TreeNode(i);
queue.push(root);
while (!queue.empty()) {
int size = queue.size();
i++;
if (i > depth) {
break ;
}
else {
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 root;
}
public :
static void inOrderTraversal(TreeNode* node) {
if (node == NULL)
return ;
inOrderTraversal(node->left);
cout << node->val << " " ;
inOrderTraversal(node->right);
}
};
int main() {
int N = 2;
TreeNode* binaryTreeRoot
= BinaryTree::perfectBinaryTree(N);
BinaryTree::inOrderTraversal(binaryTreeRoot);
return 0;
}
|
Java
import java.util.*;
class TreeNode {
public long val;
public TreeNode left;
public TreeNode right;
public TreeNode( long x)
{
val = x;
left = null ;
right = null ;
}
}
class BinaryTree {
public static TreeNode perfectBinaryTree( int depth)
{
if (depth == 0 )
return new TreeNode( 0 );
Queue<TreeNode> queue
= new LinkedList<>();
long i = 0 ;
TreeNode root = new TreeNode(i);
queue.add(root);
while (!queue.isEmpty()) {
int size = queue.size();
i++;
if (i > depth) {
break ;
}
else {
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 root;
}
public static void inOrderTraversal(TreeNode node)
{
if (node == null )
return ;
inOrderTraversal(node.left);
System.out.print(node.val + " " );
inOrderTraversal(node.right);
}
public static void main(String[] args)
{
int N = 2 ;
TreeNode binaryTreeRoot
= perfectBinaryTree(N);
inOrderTraversal(binaryTreeRoot);
}
}
|
Python3
class TreeNode:
def __init__( self , x):
self .val = x
self .left = None
self .right = None
class BinaryTree:
def perfectBinaryTree( self , depth):
if depth = = 0 :
return TreeNode( 0 )
queue = []
i = 0
root = TreeNode(i)
queue.append(root)
while len (queue) > 0 :
size = len (queue)
i + = 1
if i > depth:
break
else :
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 root
def inOrderTraversal( self , node):
if node is None :
return
self .inOrderTraversal(node.left)
print (node.val, end = " " )
self .inOrderTraversal(node.right)
def main( self ):
N = 2
binaryTreeRoot = self .perfectBinaryTree(N)
self .inOrderTraversal(binaryTreeRoot)
bTree = BinaryTree()
bTree.main()
|
C#
using System;
using System.Collections.Generic;
public class TreeNode {
public long val;
public TreeNode left;
public TreeNode right;
public TreeNode( long x) {
val = x;
left = null ;
right = null ;
}
}
public class BinaryTree {
public static TreeNode perfectBinaryTree( int depth) {
if (depth == 0)
return new TreeNode(0);
Queue<TreeNode> queue = new Queue<TreeNode>();
long i = 0;
TreeNode root = new TreeNode(i);
queue.Enqueue(root);
while (queue.Count!=0) {
int size = queue.Count;
i++;
if (i > depth) {
break ;
} else {
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 root;
}
public static void inOrderTraversal(TreeNode node) {
if (node == null )
return ;
inOrderTraversal(node.left);
Console.Write(node.val + " " );
inOrderTraversal(node.right);
}
public static void Main(String[] args) {
int N = 2;
TreeNode binaryTreeRoot = perfectBinaryTree(N);
inOrderTraversal(binaryTreeRoot);
}
}
|
Javascript
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);
|
Time Complexity: O(2N)
Auxiliary Space: O(2N)