609. Find Duplicate File in System

来源:互联网 发布:java arraylist源码 编辑:程序博客网 时间:2024/06/05 04:55

609. Find Duplicate File in System

1 题目

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.

2 翻译

您需要使用预订遍历方式从二叉树构造一个由圆括号和整数构成的字符串。

空节点需要用空括号对“()”表示。并且您需要省略不影响字符串和原始二叉树之间一对一映射关系的所有空括号对。

示例1:

输入:二叉树:[1,2,3,4]      1     / \    2   3   /      4     输出: “1(2(4))(3)”说明: Originallay需要为“1(2(4)())(3()())”,但是您需要省略所有不必要的空括号对。这将是“1(2(4))(3)”。

示例2:

输入:二叉树:[1,2,3,null,4]      1     / \    2   3     \        4 输出: “1(2()(4))(3)”说明:与第一个示例几乎相同,除了我们不能省略第一个括号对以打破输入和输出。

3 解题思路

参考了网上的一个不用递归的代码,使用栈来实现前序遍历,其中前序遍历中的括号处理是比较难的事情,思路是把括号和TreeNode一样丢入到栈中,代码如下

import java.util.Stack;public class main {    public static String tree2str(TreeNode t) {        String result="";        if(t==null){            return result;        }        Stack<Object> stack = new Stack<>();        stack.push(t);        while(!stack.isEmpty()){            Object temp=stack.pop();            if(temp instanceof TreeNode){                TreeNode tem=(TreeNode)temp;                result+=tem.val;                if(tem!=null){                    if(tem.right!=null){                        stack.push(")");                        stack.push(tem.right);                        stack.push("(");                        if(tem.left==null){                            stack.push(")");                            stack.push("(");                        }                    }                    if(tem.left!=null){                        stack.push(")");                        stack.push(tem.left);                        stack.push("(");                    }                }                else                    result+="("+")";            }            if(temp instanceof String){                result += (String)temp;            }        }        return result;    }    public static void main(String[] args) {        // TODO Auto-generated method stub        TreeNode t=new TreeNode(1);        TreeNode tl=new TreeNode(2);        TreeNode tr=new TreeNode(3);        t.left=tl;        t.right=tr;        tl=new TreeNode(4);        t.left.left=tl;        System.out.println(tree2str(t));        t=new TreeNode(1);        tl=new TreeNode(2);        tr=new TreeNode(3);        t.left=tl;        t.right=tr;        tr=new TreeNode(4);        t.left.right=tr;        System.out.println(tree2str(t));    }}

写完之后发现时间非常慢,和我看到的代码比慢了很多,找原因发现在于StringBuilder和String类的使用上,String不仅更占用空间,而且也更占用时间,参考代码


欢迎加入中科院开源软件自习室:631696396

欢迎加入中科院开源软件自习室

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 公司不发高温费怎么办 低保户没低保证怎么办 鸿运当头花干了怎么办 鸿运当头花谢了怎么办 鸿运当头花不红怎么办 众筹项目失败了怎么办 孕妇头发白了怎么办呢 猕猴桃坏了吃了怎么办 猕猴桃买的太硬怎么办 金艳猕猴桃很硬怎么办 猕猴桃又硬又酸怎么办 2岁宝宝生长缓慢怎么办 婴儿体重长得慢怎么办 人吃了乌药中毒怎么办 水中的氧气过少怎么办 怀孕9个月血糖高怎么办 怀孕的人血糖高怎么办 来月经很痛该怎么办 月经来了好痛怎么办 花呗额度用完了怎么办 老人长期卧床长了褥疮怎么办 5岁宝宝嗓子哑了怎么办 2岁宝宝喉咙嘶哑怎么办 1岁宝宝嗓子哑了怎么办 3个月婴儿嗓子哑怎么办 小孩一感冒就喘怎么办 狗打双黄连过敏怎么办 过敏怎么办怎么好的快 铁木砧板变形了怎么办 水泥地面起沙严重怎么办 横厅沙发不靠墙怎么办 吃多了胃疼怎么办 龙血树叶子干枯怎么办 龙血树叶尖发黄怎么办 茶花叶子掉光了怎么办 鸡虱子爬人身上怎么办 鸡的身上有虱子怎么办 鸡跳蚤在人身上怎么办 猫虱子到人身上怎么办 大人头上长虱子怎么办 头发上有剪发虫怎么办