331. Verify Preorder Serialization of a Binary Tree
2016-03-21 11:12
183 查看
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as
For example, the above binary tree can be serialized to the string
where
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as
Example 1:
Return
Example 2:
Return
Example 3:
Return
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
迭代迭代迭代递归递归递归啊啊啊!!!
#.
_9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # #
For example, the above binary tree can be serialized to the string
"9,3,4,#,#,1,#,#,2,#,6,#,#",
where
#represents a null node.
Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree.
Each comma separated value in the string must be either an integer or a character
'#'representing
nullpointer.
You may assume that the input format is always valid, for example it could never contain two consecutive commas such as
"1,,3".
Example 1:
"9,3,4,#,#,1,#,#,2,#,6,#,#"
Return
true
Example 2:
"1,#"
Return
false
Example 3:
"9,#,#,1"
Return
false
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
迭代迭代迭代递归递归递归啊啊啊!!!
public class Solution { private String [] str; private int i; private boolean test(){ if(i>=str.length)return false; if(str[i].equals("#"))return true; i++; if(test()==false)return false; i++; if(test()==false)return false; return true; } public boolean isValidSerialization(String preorder) { i=0; str=preorder.split(","); boolean t = test(); if(t==false)return false; if(i==str.length-1)return true; return false; } }
相关文章推荐
- 华为OJ--整型数排序
- PHP 获取指定URl页面中所有链接
- js调用中文乱码问题
- webpack 独立打包 css 文件
- QQ日志
- iOS沙盒与清除缓存
- 如何在HTTP客户端与服务器端之间保持状态
- ECSHOP取得国家列表,如果有收货人列表,取得省市区列表
- dedecms 调用栏目(分类)的文章总数
- Notice: Undefined offset: 1 in D:\\wwwroot\\wr\\askseo\\404.php on line 5
- DedeCMS下data日志mysql_error_trace.inc暴露后台地址
- [dede漏洞]dede 爆后台 路径,dede后台地址查询
- 织梦某处设计缺陷导致后台地址泄露 | WooYun-2014-76556 | WooYun
- 百度站长平台“爆”dede后台直取webshell
- DEDECMS 爆严重安全漏洞 免账号密码直接进入后台
- 织梦dedecms生成栏目HTML缓慢的一个可能原因及解决方法
- Dedecms生成速度慢的解决方案
- 简单实用方法提高织梦dedecms后台htmls生成速度2倍不是吹牛
- ueditor默认字体和字号修改
- Excel光标不能移动单元格,移动时总是移动整个表格