https://leetcode-cn.com/problems/construct-string-from-binary-tree/
class Solution {
public:
string tree2str(TreeNode* t) {
if (!t) return "";
string res = to_string(t->val);
if (!t->left && !t->right) return res;
res += "(" + tree2str(t->left) + ")";
if (t->right) res += "(" + tree2str(t->right) + ")";
return res;
}
};
if (!t->left && !t->right) return res; 这里判断根节点至少有左和右子节点。
res += "(" + tree2str(t->left) + ")"; 这里只看里根节点下第一个左节点啊?
怎么玩成整个树的遍历的呢?