public TreeNode flattenRec(TreeNode root){// return the link tail
        if(root == null) return null;
        if(root.left == null && root.right == null){
            return root;
        }else if(root.left == null){
            return flattenRec(root.right);
        }else if(root.right == null){
            TreeNode node = flattenRec(root.left);
            root.right = root.left;
            root.left = null;
            return node;
        }else {
            TreeNode node = flattenRec(root.left);
            TreeNode right = root.right;
            root.right = root.left;
            node.right = right;
            root.left = null;
            return flattenRec(right);
        }
    }