331. Verify Preorder Serialization of a Binary Tree
来源:互联网 发布:js简单算法 编辑:程序博客网 时间:2024/05/01 20:03
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 #
.
_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 null
pointer.
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; }}
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- Verify Preorder Serialization of a Binary Tree
- 331. Verify Preorder Serialization of a Binary Tree
- leetcode 331. Verify Preorder Serialization of a Binary Tree
- [LeetCode]331. Verify Preorder Serialization of a Binary Tree
- 331. Verify Preorder Serialization of a Binary Tree
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree
- 331. Verify Preorder Serialization of a Binary Tree
- LeetCode 331. Verify Preorder Serialization of a Binary Tree
- LeetCode *** 331. Verify Preorder Serialization of a Binary Tree
- 修改phpMyAdmin导入SQL文件的大小限制
- IOS开发他山之石系列-安装Alcatraz
- 织梦dede更换ueditor1.4.3,及单页栏目编辑器栏目内容、单页无法保存(原创最新)
- dedecms 调用栏目(分类)的文章总数
- Notice: Undefined offset: 1 in D:\\wwwroot\\wr\\askseo\\404.php on line 5
- 331. Verify Preorder Serialization of a Binary Tree
- DedeCMS下data日志mysql_error_trace.inc暴露后台地址
- [dede漏洞]dede 爆后台 路径,dede后台地址查询
- 织梦某处设计缺陷导致后台地址泄露 | WooYun-2014-76556 | WooYun
- 百度站长平台“爆”dede后台直取webshell
- js获取对象的属性和方法总结
- DEDECMS 爆严重安全漏洞 免账号密码直接进入后台
- 织梦dedecms生成栏目HTML缓慢的一个可能原因及解决方法
- js调用中文乱码问题