Build a binary tree from a list of tuples


  • 0

    Can anyone improve on this?

    public class Node
            {
                public int val { get; set; }
                public Node left { get; set; }
                public Node right { get; set; }
                public bool hasParent { get; set; }
                public Node(int Val)
                {
                    val = Val;
                    left = null;
                    right = null;
                    hasParent = false;
                }
    
            }
    
            static void Main(string[] args)
            {
                //      10
                //   5         20
                //1    2  15    17
    
                KeyValuePair<int, int> kvp = new KeyValuePair<int, int>(5,1);
                KeyValuePair<int, int> kvp2 = new KeyValuePair<int, int>(5, 2);
                KeyValuePair<int, int> kvp3 = new KeyValuePair<int, int>(20, 15);
                KeyValuePair<int, int> kvp4 = new KeyValuePair<int, int>(20, 17);
                KeyValuePair<int, int> kvp5 = new KeyValuePair<int, int>(10, 5);
                KeyValuePair<int, int> kvp6 = new KeyValuePair<int, int>(10, 20);
                List<KeyValuePair<int, int>> list = new List<KeyValuePair<int, int>>();
                list.Add(kvp);
                list.Add(kvp2);
                list.Add(kvp3);
                list.Add(kvp4);
                list.Add(kvp5);
                list.Add(kvp6);
                Node node = BuildTree(list);
                
            }
    
            public static Node BuildTree(List<KeyValuePair<int,int>> list)
            {
                Dictionary<int, Node> nodes = new Dictionary<int, Node>();
    
                foreach (KeyValuePair<int, int> kvp in list)
                {
                    //      10
                   //   5         20
                  //1    2  15    17
    
                    Node parent = null;
                    Node child = null;
                    if (!nodes.TryGetValue(kvp.Key, out parent) && !nodes.TryGetValue(kvp.Value, out child))
                    {
                        parent = new Node(kvp.Key);     //20 - 15, //5 - 1
                        child = new Node(kvp.Value);
                        child.hasParent = true;
                        parent.left = child;
                        nodes.Add(kvp.Key, parent);
                        nodes.Add(kvp.Value, child);
                    }
                    else if (nodes.TryGetValue(kvp.Key, out parent) && !nodes.TryGetValue(kvp.Value, out child))
                    {
                        child = new Node(kvp.Value);   //20 - 17, 5-2
                        child.hasParent = true;
                        nodes[kvp.Key].right = child;
                        nodes.Add(kvp.Value, child);
                    }
                    else if (!nodes.TryGetValue(kvp.Key, out parent) && nodes.TryGetValue(kvp.Value, out child))
                    {
                        parent = new Node(kvp.Key);  //10 - 5                   
                        parent.left = child;
                        nodes[kvp.Value].hasParent = true;
                        nodes.Add(kvp.Key, parent);
                    }
                    else if (nodes.TryGetValue(kvp.Key, out parent) && nodes.TryGetValue(kvp.Value, out child))
                    {
                        nodes[kvp.Key].right = nodes[kvp.Value]; //10 -20
                        nodes[kvp.Value].hasParent = true;
                    }
                }
    
                foreach(KeyValuePair<int, Node> kvp in nodes)
                {
                    if(kvp.Value.hasParent == false)
                    {
                        return kvp.Value;
                    }
                }
                return null;
            }
        }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.