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
阅读全文
0 0
- 609. Find Duplicate File in System
- 609. Find Duplicate File in System
- 609. Find Duplicate File in System
- 609. Find Duplicate File in System
- 609. Find Duplicate File in System
- 609. Find Duplicate File in System
- leetcode 609. Find Duplicate File in System
- [leetcode]609. Find Duplicate File in System
- Leetcode [609. Find Duplicate File in System]
- 609. Find Duplicate File in System
- 609. Find Duplicate File in System
- Find Duplicate File in System
- LeetCode 609: Find Duplicate File in System(python)
- Find Duplicate File in System问题及解法
- Leetocode之Find Duplicate File in System 问题
- leetcode 609. Find Duplicate File in System C++stringstream的一个很好地示范
- Find in file
- Find in Large File
- android recycleview实现两列不滑动(nestscrollview嵌套)
- 强烈推荐--理解矩阵(孟岩)
- ios代码设置圆角
- VIM配置
- 取数组前几个
- 609. Find Duplicate File in System
- linux下固态硬盘ssd优化
- 500 G JAVA视频网盘分享
- linux下mysql配置文件my.cnf详解
- 可穿带物品ItemWearable
- hdoj1084 What Is Your Grade?(思路很简单)
- aqtest 003
- 可控硅基本概念
- JAVA学习之 36-JAVA中的字符串知识点整理(三)