331. Verify Preorder Serialization of a Binary Tree
来源:互联网 发布:北京美工培训班学费 编辑:程序博客网 时间:2024/04/18 08:26
题目:
用特殊的方法先序遍历而成的字符串,判断它可不可以恢复成一个二叉树。
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.
思路:- 第一种思路(我的):一个可以恢复成二叉树的字串-根节点+左子树的字串+右子树的字串,用递归的方法来判断
- 第二种思路(男朋友的):通过二叉树的性质,所有二叉树中Null指针的个数=节点个数+1。因为一棵树要增加一个节点,必然是在null指针的地方增加一个叶子结点,也就是毁掉一个null指针的同时带来两个null指针,意味着每增加一个节点,增加一个null指针。然而最开始一颗空树本来就有一个null指针,因此二叉树中null指针的个数等于节点数+1。从头开始扫描这个字串,如果途中#的个数超了,或者字符串扫描完不满足等式则返回false。
程序:
class pointer {int val = 0;}public class Solution {public boolean helper(String[] pre, pointer p) {if(p.val > pre.length - 1) return false;if(pre[p.val].equals("#")) {p.val++; return true;}else {p.val++;return helper(pre, p) && helper(pre, p);}} public boolean isValidSerialization(String preorder) { if(preorder == null ) return false; String[] pre = preorder.split(","); pointer p = new pointer(); return helper(pre, p) && p.val == pre.length; }}
下面是一个感觉很简洁的程序,来自leetcode discussion
https://leetcode.com/discuss/83824/7-lines-easy-java-solution
public boolean isValidSerialization(String preorder) { String[] nodes = preorder.split(","); int diff = 1; for (String node: nodes) { if (--diff < 0) return false; if (!node.equals("#")) diff += 2; } return diff == 0;}
0 0
- 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
- 安卓开发——DisplayMetrics类
- FZUOJ 1402 猪的安家 (中国剩余定理 互质)
- 每个痛苦的过程都是你进步的阶梯
- HDU 2647 Reward(拓扑排序)
- 初来乍到,发博一篇,关于Fortran和Matlab
- 331. Verify Preorder Serialization of a Binary Tree
- Maven学习 (六) 搭建多模块企业级项目
- Fragment使用(二)
- 【Android开发小记--6】动画--属性动画以及Fragment切换动画(3D)
- 【连通分量】BZOJ 1051:[HAOI2006]受欢迎的牛
- 【RxJava】Observable基本方法
- HDOJ 1224 Free DIY Tour (Floyd + 打印路径)
- ASP.NET中FORM验证中的cookieless属性
- 检测远程端口是否打开