G面经Prepare: Valid Preorder traversal serialized String
求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如
A) 9 3 4 # # 1 # # 2 # 6 # #是对的,因为表示
9
/ \
3 2
/ \ \
4 1 6
B ) 9 3 4 # # 1 # #就是错的,因为无法反构造回一棵树
我觉得可以在字符串里找"n##"这种结构(对应tree里两个children都是Null的叶节点),找到之后就把"n##"改写成"#",也就是把找到的那个末端的子树想想成null,最后字符串变成"#"的就是valid,否则就invalid
比如 A) 9 3 "4 # #" "1 # #" 2 # "6 # #" ---> 9 3 # # 2 # # ---> 9 "3 # #" "2 # #" ---> 9 # # ---> "9 # #" ---> "#"
B) 9 3 "4 # #" "1 # #" ---> 9 3 # # ---> 9 "3 # #" ---> 9 # (没有"n##"结构了,return false)
也可用stack来这样做,当前如果是#,stack peek如果也是#且size>=2,就pop两次,且再check当前这个#
package PreorderValidSerialized;
import java.util.*; public class Solution {
public boolean check(String str) {
String[] strs = str.split(" ");
Stack<String> stack = new Stack<String>();
for (int i=0; i<strs.length; i++) {
if (stack.size()>=2 && strs[i].equals("#") && stack.peek().equals("#")) {
stack.pop();
stack.pop();
i--;
}
else stack.push(strs[i]);
}
if (stack.size()==1 && stack.peek().equals("#")) return true;
else return false;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
boolean res = sol.check("9 3 4 # # 1 # # 2 # 6 # # #");
if (res) System.out.println("true");
else System.out.println("false");
} }
G面经Prepare: Valid Preorder traversal serialized String的更多相关文章
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- Construct BST from given preorder traversal
Given preorder traversal of a binary search tree, construct the BST. For example, if the given trave ...
- Binary Tree Inorder/Preorder Traversal 返回中序和前序/遍历二叉树的元素集合
给定一个二叉树,以集合方式返回其中序/先序方式遍历的所有元素. 有两种方法,一种是经典的中序/先序方式的经典递归方式,另一种可以结合栈来实现非递归 Given a binary tree, retur ...
- [LeetCode] Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【leetcode】Binary Tree Preorder Traversal (middle)★
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- [LeetCode] Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- 【LeetCode】Binary Tree Preorder Traversal
Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...
- LintCode Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Given: 1 / \ 2 3 / \ 4 5 re ...
随机推荐
- xml追加节点
添加方法 public void XmlAppend(VisitM vm) { XmlDocument xmldoc = new XmlDocument(); string path = Server ...
- 5.PHP内核探索:多进程/线程的SAPI生命周期
多进程的SAPI生命周期 通常PHP是编译为apache的一个模块来处理PHP请求.Apache一般会采用多进程模式, Apache启动后会fork出多个子进程,每个进程的内存空间独立,每个子进程都会 ...
- Why Stored Procedures?
http://www.w3resource.com/mysql/mysql-procedure.php Stored procedures are fast. MySQL server takes s ...
- phone number is not known @w@ have no phone, and thus no phone number
http://dev.mysql.com/doc/refman/5.7/en/problems-with-null.html B.5.4.3 Problems with NULL Values The ...
- 不遗留问题-menu数据拼装-2
$res = array(); foreach($idlist_1 as $id1) { $tmp = array(); $tmp1 = array(); $tmp1[] = $id1; foreac ...
- 安装faac编译问题
mpeg4ip.h:126: error: new declaration ‘char* strcasestr(const char*, const char*)’分类: Linux开发相关 2012 ...
- WIN API 擦除所绘图像
COLORREF circle_color = RGB(0, 105, 255); //获取窗口DC HDC hdc = GetDC(hWnd_); //背景色透明 SetBkMode(hdc, TR ...
- 答CsdnBlogger问-关于安卓入行和开发问题
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 问1:请问大牛对功能和框架的认识有哪些?(提问者:执笔记忆的空白) 比如对于一个小公司来说,什么样的 ...
- 汇编ASCII大小写转换(字符串长度$的用法)
data segment first db "BaSiC" db "iNfOrMaTiOn" len equ $-first ;×Ö·û´®³¤¶È ends ...
- Cocos2d-JS工程中的文件结构
res文件夹存放资源文件 src文件夹是主要的程序代码 app.js是实现游戏场景的JavaScript文件 resource.js在src文件夹中,定义资源对应的变量 config.json保存模拟 ...