コード例 #1
0
ファイル: Program.cs プロジェクト: vshardul/RandomStuff
        private static TreeNode CreateBST(Node head)
        {
            TreeNode root = null;

            while (head != null)
            {
                InsertBST(ref root, head.value);
                head = head.next;
            }

            return root;
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: vshardul/RandomStuff
        private static TreeNode CreateTree(Node current)
        {
            if (current == null)
            {
                return null;
            }

            var root = new TreeNode { value = current.value };
            var currentT = root;
            current = current.next;

            int i = 1;
            var treeQueue = new Queue<TreeNode>();
            while (current != null)
            {
                var treeNode = new TreeNode { value = current.value };
                current = current.next;
                if (i == 1)
                {
                    currentT.left = treeNode;
                    treeQueue.Enqueue(currentT.left);
                    i++;
                }
                else if (i == 2)
                {
                    currentT.right = treeNode;
                    treeQueue.Enqueue(currentT.right);
                    i++;
                }

                if(i >= 3)
                {
                    i = 1;
                    currentT = treeQueue.Dequeue();
                }
            }

            return root;
        }