Given the root of a binary tree, construct a string consisting of parenthesis and integers from a binary tree with the preorder traversal way, and return it.
Omit all the empty parenthesis pairs that do not affect the one-to-one mapping relationship between the string and the original binary tree.
Example 1:

Input: root = [1,2,3,4]
Output: "1(2(4))(3)"
Explanation: Originally, it needs to be "1(2(4)())(3()())", but you need to omit all the unnecessary empty parenthesis pairs. And it will be "1(2(4))(3)"
Example 2:

Input: root = [1,2,3,null,4]
Output: "1(2()(4))(3)"
Explanation: Almost the same as the first example, except we cannot omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
Constraints:
[1, 104].1000 <= Node.val <= 1000// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
// Option 타입은 열거형으로 "Rc<RefCell<TreeNode>>" 값의 존재 여부를 알려준다.
pub fn tree2str(root: Option<Rc<RefCell<TreeNode>>>) -> String {
match root {
// root 에 값이 있다면, 값을 출력하고 자식 노드 순회
Some(node) => {
let node_ref = node.borrow();
let mut result = node_ref.val.to_string();
if node_ref.left.is_some() || node_ref.right.is_some() {
result.push('(');
result.push_str(&Solution::tree2str(node_ref.left.clone()));
result.push(')');
if node_ref.right.is_some() {
result.push('(');
result.push_str(&Solution::tree2str(node_ref.right.clone()));
result.push(')');
}
}
result
}
// root 가 비어 있다면, 빈 문자열 출력
None => String::new(),
}
}
}