LeetCode Construct String from Binary Tree 根据二叉树创建字符串

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)"

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

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

示例 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)"

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

题解:给定一棵二叉树,通过二叉树的前序遍历,将其前序遍历放入到一个string中。此题一看就可以用递归来做,首先是放入根节点,然后根据先序遍历的特点,先遍历左子树,如果左子树不为空,那么先将'('压入到一个list中,然后再递归该左子树,递归结束后,再将')'压入到list中;以此类推。但是这里必须考虑一种特殊情况,即当根节点的左子树为空,而右子树不为空时,应该要压入'()',然后再根据遍历右子树的节点,压入到list中,而不能直接不讲'()'压入list中;而且这里有一个小小的tip,最好将节点的值转化为string,然后压入list中,而不要转化为character;其次,在最后将list保存到string中时,不要直接读入string,因为通过string += list.get(i)的方式,每次都会在内存中新生成一个string;因为,最好是通过新生成一个stringbuilder对象,通过stringbuilder.append()方式累加,然后在通过stringbuilder.tostring()方式来将其转化为string。

public static String tree2str(TreeNode t)
    {
        if(t == null)
            return "";
        List<String> list = new ArrayList<>();
        transfer(t,list);
        StringBuilder res = new StringBuilder();  //这里不能用strin,否则空间复杂度太高,因为每次都会生成一个string类型
        while(!list.isEmpty())
        {
            res.append(list.get(0));
            list.remove(0);
        }
        return res.toString();
    }
    public static void transfer(TreeNode root,List<String> list)
    {
        if(root == null)       //递归的思想来做
            return;
        list.add(String.valueOf(root.val));
        if(root.left != null)
        {
            list.add("(");
            transfer(root.left,list);
            list.add(")");
        }
        if(root.left == null && root.right != null)   //这种情况得另外考虑,即当左子树为空,而右子树不为空时,还是需要将()压入到list中
        {
            list.add("(");
            list.add(")");
        }
        if(root.right != null)
        {
            list.add("(");
            transfer(root.right,list);
            list.add(")");
        }
    }

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值