【Leetcode】606. 根据二叉树创建字符串

本文介绍了一种算法,将二叉树转换为由括号和整数组成的字符串,通过前序遍历实现,并提供了三种不同的实现方法,包括递归和非递归方式,以减少冗余括号。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

QUESTION

easy

题目描述

你需要采用前序遍历的方式,将一个二叉树转换成一个由括号和整数组成的字符串。

空节点则用一对空括号 "()" 表示。而且你需要省略所有不影响字符串与原始二叉树之间的一对一映射关系的空括号对。

示例 1:

输入: 二叉树: [1,2,3,4]
       1
     /   \
    2     3
   /
  4

输出: "1(2(4))(3)"

解释: 原本将是“1(2(4)())(3())”,
在你省略所有不必要的空括号对之后,
它将是“1(2(4))(3)”。

示例 2:

输入: 二叉树: [1,2,3,null,4]
       1
     /   \
    2     3
     \  
      4

输出: "1(2()(4))(3)"

解释: 和第一个示例相似,
除了我们不能省略第一个对括号来中断输入和输出之间的一对一映射关系。

说明


SOLUTION

() 冗余的情况有两种:

  • 左右都为空,root()(),两个冗余
  • 左不为空,右为空,root(left)(),一个冗余

方法一

最容易理解的就把它分成四种情况来做

class Solution {
public:
    string tree2str(TreeNode* t) {
        string res = "";
        preorder(t, res);
        return res;
    }
    void preorder(TreeNode* node, string &res){
        if(node == nullptr) return;
        res += to_string(node->val);
        if(node->left == nullptr && node->right == nullptr) return;
        else if(node->left == nullptr && node->right != nullptr) {
            res += "()(";
            preorder(node->right, res);
            res += ')';
        }
        else if(node->left != nullptr && node->right == nullptr) {
            res += '(';
            preorder(node->left, res);
            res += ')';
        }
        else {
            res += '(';
            preorder(node->left, res);
            res += ")(";
            preorder(node->right, res);
            res += ')';
        }
    }
};

可以写的再简洁点

class Solution {
public:
    string tree2str(TreeNode* t) {
        string res = "";
        preorder(t, res);
        return res;
    }
    void preorder(TreeNode* node, string &res){
        if(node == nullptr) return;
        res += to_string(node->val);
        if(node->right == nullptr){
            if(node->left != nullptr) {
                res += "(";
                preorder(node->left, res);
                res += ")";
            }
        }
        else {
            res += "(";
            preorder(node->left, res);
            res += ")(";
            preorder(node->right, res);
            res += ")";
        }
    }
};

转载方法

转自 Grandyang 的博客

方法二

同方法一,写法不同

class Solution {
public:
    string tree2str(TreeNode* t) {
        if (!t) return "";
        string res = "";
        helper(t, res);
        return string(res.begin() + 1, res.end() - 1);
    }
    void helper(TreeNode* t, string& res) {
        if (!t) return;
        res += "(" + to_string(t->val);
        if (!t->left && t->right) res += "()";
        helper(t->left, res);
        helper(t->right, res);
        res += ")";
    }
};

方法三

不使用额外的函数,但本质相同

class Solution {
public:
    string tree2str(TreeNode* t) {
        if (!t) return "";
        string res = to_string(t->val);
        string left = tree2str(t->left), right = tree2str(t->right);
        if (left == "" && right == "") return res;
        if (left == "") return res + "()" + "(" + right + ")";
        if (right == "") return res + "(" + left + ")";
        return res + "(" + left + ")" + "(" + right + ")";
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值