
function tree2str(root: TreeNode | null): string {
if(!root) return ''
let str = `${root.val}`
if(root.left && root.right) {
str += `(${tree2str(root.left)})`
str += `(${tree2str(root.right)})`
} else if(root.left) {
str += `(${tree2str(root.left)})`
} else if(root.right) {
str += '()'
str += `(${tree2str(root.right)})`
}
return str
};