public void connect_method2 (TreeLinkNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if (root == null) return;

        TreeLinkNode cur = root;
        while (cur != null) {
            TreeLinkNode nextHead = null;
            TreeLinkNode pre = null;

            for (; cur != null; cur = cur.next) {
                if (nextHead == null) nextHead = (cur.left != null) ? cur.left : cur.right;
                if (cur.left != null) {
                    if (pre != null) pre.next = cur.left;
                    pre = cur.left;
                }
                if (cur.right != null) {
                    if (pre != null) pre.next = cur.right;
                    pre = cur.right;
                }
            }
            cur = nextHead;
        }
    }