## C# || How To Construct Binary Tree From Preorder And Inorder Traversal Using C# The following is a module with functions which demonstrates how to construct a binary tree from pre order and in order traversal using C#.

1. Build Tree – Problem Statement

Given two integer arrays preorder and inorder where preorder is the preorder traversal of a binary tree and inorder is the inorder traversal of the same tree, construct and return the binary tree.

Example 1: ``` Input: preorder = [3,9,20,15,7], inorder = [9,3,15,20,7] Output: [3,9,20,null,null,15,7] ```

Example 2:

``` Input: preorder = [-1], inorder = [-1] Output: [-1] ```

2. Build Tree – Solution

The following is a solution which demonstrates how to construct a binary tree from pre order and in order traversal.

``` 2. Build Tree - Solution C# // ============================================================================ // Author: Kenneth Perkins // Date: Nov 20, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to build binary tree from pre order and in order // ============================================================================ /** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { * this.val = val; * this.left = left; * this.right = right; * } * } */ public class Solution { private int preorderIndex; private Dictionary<int, int> inorderIndexMap; public TreeNode BuildTree(int[] preorder, int[] inorder) { preorderIndex = 0; // Build a hashmap to store value -> its index relations inorderIndexMap = new Dictionary<int, int>(); for (int index = 0; index < inorder.Length; ++index) { inorderIndexMap[inorder[index]] = index; } return ArrayToTree(preorder, 0, preorder.Length - 1); } private TreeNode ArrayToTree(int[] preorder, int start, int end) { // If there are no elements to construct the tree if (start > end) { return null; } // Select the preorder_index element as the root and increment it var rootValue = preorder[preorderIndex++]; var root = new TreeNode(rootValue); // Get current positon var curPos = inorderIndexMap[rootValue]; // Build left and right subtree // excluding inorderIndexMap[rootValue] element because it's the root root.left = ArrayToTree(preorder, start, curPos - 1); root.right = ArrayToTree(preorder, curPos + 1, end); return root; } }// http://programmingnotes.org/ 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 // ============================================================================//    Author: Kenneth Perkins//    Date:   Nov 20, 2021//    Taken From: http://programmingnotes.org///    File:  Solution.cs//    Description: Demonstrates how to build binary tree from pre order and in order// ============================================================================/** * Definition for a binary tree node. * public class TreeNode { *     public int val; *     public TreeNode left; *     public TreeNode right; *     public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) { *         this.val = val; *         this.left = left; *         this.right = right; *     } * } */public class Solution {    private int preorderIndex;    private Dictionary<int, int> inorderIndexMap;     public TreeNode BuildTree(int[] preorder, int[] inorder) {        preorderIndex = 0;         // Build a hashmap to store value -> its index relations        inorderIndexMap = new Dictionary<int, int>();        for (int index = 0; index < inorder.Length; ++index) {            inorderIndexMap[inorder[index]] = index;        }        return ArrayToTree(preorder, 0, preorder.Length - 1);    }     private TreeNode ArrayToTree(int[] preorder, int start, int end) {        // If there are no elements to construct the tree        if (start > end) {            return null;        }         // Select the preorder_index element as the root and increment it        var rootValue = preorder[preorderIndex++];        var root = new TreeNode(rootValue);         // Get current positon        var curPos = inorderIndexMap[rootValue];         // Build left and right subtree        // excluding inorderIndexMap[rootValue] element because it's the root        root.left = ArrayToTree(preorder, start, curPos - 1);        root.right = ArrayToTree(preorder, curPos + 1, end);        return root;    }}// http://programmingnotes.org/ ```

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

Once compiled, you should get this as your output for the example cases:

``` [3,9,20,null,null,15,7] [-1] ```