Leetcode -- Verify Preorder Serialization of a Binary Tree
来源:互联网 发布:linux中expect 编辑:程序博客网 时间:2024/05/24 02:14
题目描述
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
解答:
public static boolean isValidSerialization(String preorder) {List<String> stack = new ArrayList<String>();String[] arr = preorder.split(",");for (int i = 0; i < arr.length; i++) {stack.add(arr[i]);while (stack.size() >= 3 && stack.get(stack.size() - 1).equals("#") && stack.get(stack.size() - 2).equals("#") && !stack.get(stack.size() - 3).equals("#")) {stack.remove(stack.size() - 1);stack.remove(stack.size() - 1);stack.remove(stack.size() - 1);stack.add("#");}}if (stack.size() == 1 && stack.get(0).equals("#")) return true;else return false;}解法二:
public static 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;}
refer:
- leetcode Verify Preorder Serialization of a Binary Tree
- Leetcode: Verify Preorder Serialization of a Binary Tree
- LeetCode 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
- [leetcode] Verify Preorder Serialization of a Binary Tree
- [Leetcode]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
- LeetCode 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
- [LeetCode]Verify Preorder Serialization of a Binary Tree
- LeetCode 331. Verify Preorder Serialization of a Binary Tree
- leetcode verify Preorder Serialization of a Binary Tree
- 【Leetcode】Verify Preorder Serialization of a Binary Tree
- LeetCode No331. Verify Preorder Serialization of a Binary Tree
- LeetCode:Verify Preorder Serialization of a Binary Tree
- 网络爬虫,用C#做一个网络爬虫demo,功能有保存网页、图片、js文件、等等其他的文件。有界面显示,有代码注释。
- Process terminated with status 255 错误,首先检查错误
- iOS富文本设置样式,插入图片,点击事件
- 在多线程情况下,block比delegate有优势。
- hihocoder 1384 Genius ACM
- Leetcode -- Verify Preorder Serialization of a Binary Tree
- PCB课程设计5
- Uva 11478 Halum(差分约束系统 + 二分)
- Address already in use: JVM_Bind(端口冲突)
- ElasticSearch windows 单机多节点
- 伸展树splay之求区间极值
- kiwi简介
- RecyclerView使用问题集合http://www.jianshu.com/p/333fe22cabc6
- Linux信号系统详解