Skip to content

Commit e716778

Browse files
authored
Add files via upload
1 parent 4f655f4 commit e716778

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Given binary tree [3,9,20,null,null,15,7],
3+
4+
3
5+
/ \
6+
9 20
7+
/ \
8+
15 7
9+
*/
10+
11+
using System;
12+
using System.Collections.Generic;
13+
using System.Linq;
14+
using System.Text;
15+
using System.Threading.Tasks;
16+
17+
namespace binarytree_create_binarytree_with_nullvaluesCS
18+
{
19+
public class TreeNode
20+
{
21+
public int val;
22+
public TreeNode left;
23+
public TreeNode right;
24+
public TreeNode(int value ) { val = value; }
25+
}
26+
class Program
27+
{
28+
static TreeNode build_tree(char[] level_order_collection, int start_index, int end_index)
29+
{
30+
// a tree is always built up in bottom-up structure
31+
TreeNode root = null;
32+
// initial referance set to null
33+
34+
if (start_index <= end_index)
35+
{
36+
// node to be created has not over-shooted the available range
37+
38+
root = new TreeNode(level_order_collection[start_index]);
39+
40+
root.left = build_tree(level_order_collection, (2 * start_index) + 1, end_index);
41+
root.right = build_tree(level_order_collection, (2 * start_index) + 2, end_index);
42+
// recursively call the same tree build function to create left sub-tree and right sub tree
43+
44+
}
45+
return root;
46+
}
47+
static void Main(string[] args)
48+
{
49+
Nullable<int> level_order = new Nullable<int> { 3, 9, 20, null, null, 15, 7 };
50+
// level order traversal for the tree nodes provided
51+
}
52+
}
53+
}

0 commit comments

Comments
 (0)