示例#1
0
        public Node TreeToDoublyList(Node root)
        {
            if (root == null)
            {
                return(null);
            }

            ReturnTy re   = BinaryConverToList(root);
            Node     head = re.headNode;
            Node     tail = re.tailNode;

            head.left  = tail;
            tail.right = head;
            return(head);
        }
示例#2
0
        public ReturnTy BinaryConverToList(Node root)
        {
            if (root == null)
            {
                //return null;
                return(new ReturnTy(null, null));
            }

            ReturnTy leftReturn  = BinaryConverToList(root.left);
            ReturnTy rightReturn = BinaryConverToList(root.right);

            if (leftReturn.tailNode != null)
            {
                root.left = leftReturn.tailNode;

                leftReturn.tailNode.right = root;
            }
            else
            {
                leftReturn.headNode = root;
            }

            if (rightReturn.headNode != null)
            {
                root.right = rightReturn.headNode;


                rightReturn.headNode.left = root;
            }
            else
            {
                rightReturn.tailNode = root;
            }

            return(new ReturnTy(leftReturn.headNode, rightReturn.tailNode));
        }