leetcode: 606. Construct String from Binary Tree

题目解析:

题目链接:https://leetcode.com/problems/construct-string-from-binary-tree/description/

You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.

The null node needs to be represented by empty parenthesis pair "()". And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string and the original binary tree.

用先序遍历一个二叉树得到的整数和圆括号创建一个字符串。

空节点用"()"表示。你需要去掉那些不影响从字符串到二叉树一对一映射的空圆括号。

Example 1:

Input: Binary tree: [1,2,3,4]
       1
     /   \
    2     3
   /    
  4     

Output: "1(2(4))(3)"

Explanation: Originallay 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: Binary tree: [1,2,3,null,4]
       1
     /   \
    2     3
     \  
      4 

Output: "1(2()(4))(3)"

Explanation: Almost the same as the first example,
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.

题目给出了先序遍历的解题方法。剩下的难点就在于将括号加入遍历。看题目要求根节点不用括号,然后左子树是按照父节点的括号要包含子节点进行排列。然后主要是如何去掉不影响一一对应的空圆括号。从题目给的例子以及下面的解析中可以得出所谓不影响一一对应的空括号就是指一个节点下面右子节点为空,即右子树空括号可以省略,左子树空括号要保留

解题思路:

由于看例子可得看到根节点不能用括号包围,所以先判断一个节点是不是根节点,如果是,只将节点值放进字符串中;如果不是,先将左括号放进字符串中,然后将节点值放进字符串中,然后进行递归,递归出来的时候将对应右括号放进字符串。考虑边界条件,笔者代码如下:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    string tree2str(TreeNode* t) {
        string s;
        if(!t)
            return s;
        flagh = 0;//判断是否为根节点条件
        retree2str(t, s);
        return s;
    }
    int flagh = 0;
    void retree2str(TreeNode* t, string &s)
    {
        if(!t)
            return ;
        
        if(flagh == 0)//如果是根节点
        {
            flagh = 1;//判断条件更新
            s+=to_string(t->val);//节点值不用括号包围
            if(!t->left&&t->right)
            {
                s+="()";//左子树为空要保留空括号
                retree2str(t->right,s);
            }
            else if(t->left&&!t->right)
            {
              retree2str(t->left,s);
            }
            else
            {
            
                retree2str(t->left,s);
           
                retree2str(t->right,s);
            }
        }
        else
        {
            s += "("+to_string(t->val);//不是根节点要保留括号
            if(!t->left&&t->right)
            {
                s+="()";
                retree2str(t->right,s);
            }
            else if(t->left&&!t->right)
            {
              retree2str(t->left,s);
            }
            else
            {
            
                retree2str(t->left,s);
           
                retree2str(t->right,s);
            }
            s+=")";
        }
        return ;
        
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值