Skip to content

Instantly share code, notes, and snippets.

@PrashantUnity
Created October 5, 2022 04:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PrashantUnity/e6ad3f71a12a8e2a79ca63b191fdb37a to your computer and use it in GitHub Desktop.
Save PrashantUnity/e6ad3f71a12a8e2a79ca63b191fdb37a to your computer and use it in GitHub Desktop.
Will Help in debugging on your local computer
public class Program
{
static void Main(string[] args)
{
var arr = new int[] {1,2,3,4,5,6,7,8,9};
var tree = new LeetCodeTree().MakeTree(arr);
BFS(tree);
}
}
public class LeetCodeTree
{
int[] arr;
int m = 0;
public TreeNode MakeTree(int[] array)
{
if (array.Length == 0) return null;
arr = array;
var root = new TreeNode(arr[m++]);
for (int i = 0; i <= Math.Log(arr.Length, 2) + 1; i++)
Add(root, i, 0);
return root;
}
public void Add(TreeNode root, int depth, int i)
{
if (root == null) return;
if (i == depth - 2)
{
if (m < arr.Length)
root.left = new TreeNode(arr[m++], root.left, null);
if (m < arr.Length)
root.right = new TreeNode(arr[m++], null, root.right);
return;
}
Add(root.left, depth, i + 1);
Add(root.right, depth, i + 1);
}
}
public class TreeNode
{
public int value;
public TreeNode left;
public TreeNode right;
public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null)
{
this.value = val;
this.left = left;
this.right = right;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment