606. Construct String from Binary Tree

606. Construct String from Binary Tree
[思路]:

  1. 利用先序遍历输出二叉树中的非空节点;
  2. 但是输出必须具有唯一性;
  3. 左子树的空就不能忽略;
  4. 右子树的空可以忽略;
    string tree2str(TreeNode* t) {
        if (t == NULL) return "";
        string s = to_string(t->val);
        if (t->left) s += '(' + tree2str(t->left) + ')';
        else if (t->right) s += "()";
        if (t->right) s += '(' + tree2str(t->right) + ')';
        return s;

    }
    

你可能感兴趣的:(606. Construct String from Binary Tree)