0

Suppose we are given an array [20,8,22,5,3,4,25,null,null,10,14,null,null,null,null] and we want to construct a Binary Tree from it using Level Order , how can we do this in c# since it doesn't allow null int.

enter image description here

knowledgeseeker
  • 1,163
  • 4
  • 15
  • 32
  • How are you given the array with nulls in it if it can't contain nulls? It must be some type that allows nulls: use that type. – Welbog Apr 03 '21 at 12:33
  • interesting point .. java allows this... but c# doesn't.So if we are given this in java we could do this insertion.How would this be done in c#. I am open to modify array to achieve this. – knowledgeseeker Apr 03 '21 at 13:40
  • What is the input really? Is it a text file? JSON format? Like said above, the array notation you gave is not valid C# syntax, so that kills the question. – trincot Apr 03 '21 at 16:30

1 Answers1

0

A simple pseudo-code:

function create_node(index, arr)
    node n = node()
    if 2*index + 1 < arr.size and arr[2*index + 1] != null
        n.left = create_node(2*index + 1, arr)
    if 2*index + 2 < arr.size and arr[2*index + 1] != null
        n.right = create_node(2*index + 2, arr)

As you say, there is the issue of null. You can simply use some value outside of the range you expect - perhaps -1.