예제 #1
0
        public BinaryTreeNode convertList2Binary(BinaryTreeNode node)
        {
            Queue <BinaryTreeNode> q = new Queue <BinaryTreeNode>();

            //creates a queue to store parent values

            if (head == null)
            {
                node = null;
                return(null);
            }
            node = new BinaryTreeNode(head.data);
            q.Enqueue(node);

            head = head.next;
            while (head != null)
            {
                BinaryTreeNode parent = q.Peek();
                BinaryTreeNode pp     = q.Dequeue();

                BinaryTreeNode leftChild = null, rightChild = null;

                leftChild = new BinaryTreeNode(head.data);
                q.Enqueue(leftChild);
                head = head.next;

                if (Program.DataList.Count != 0)
                {
                    rightChild = new BinaryTreeNode(head.data);
                    q.Enqueue(rightChild);
                    head = head.next;
                }
                parent.left  = leftChild;
                parent.right = rightChild;
            }
            return(node);
        }
예제 #2
0
 public ListNode(int d)
 {
     data = d;
     next = null;
 }