public void connect(TreeLinkNode root) {
        if(root==null) {
            return;
        }

        if(root.left != null && root.right !=null) {
            root.left.next = root.right;
        }

        if(root.next!=null) {
            TreeLinkNode left, right=null;
            left = root.right==null?root.left:root.right;
            for(TreeLinkNode p=root.next; p!=null; p=p.next) {
                if(p.left!=null) {
                    right = p.left;
                    break;
                }
                if(p.right!=null) {
                    right = p.right;
                    break;
                }
            }
            if(left!=null) {
                left.next = right;
            }
        }

        connect(root.right);
        connect(root.left);
    }