public void connect(TreeLinkNode root) {
        TreeLinkNode lineStart = root;
        root.next = null;
        while (lineStart != null) {
            TreeLinkNode nextStart = null;
            TreeLinkNode it = lineStart;
            TreeLinkNode nextIt = null;
            while (it != null) {
                if (it.left != null) {
                    if (nextStart == null) {
                        nextStart = it.left;
                        nextIt = nextStart;
                    }
                    else {
                        nextIt.next = it.left;
                        nextIt = it.left;
                    }
                }
                if (it.right != null) {
                    if (nextStart == null) {
                        nextStart = it.right;
                        nextIt = nextStart;
                    }
                    else {
                        nextIt.next = it.right;
                        nextIt = it.right;
                    }
                }
                it = it.next;
            }
            lineStart = nextStart;
        }
    }