LeetCode_Stack_331. Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化(Java)【栈,字符串处理】
目录
一,题目描述
英文描述
中文描述
示例与说明
二,解题思路
1,栈合并节点
2,计算入度出度和
三,AC代码
Java
栈
计算出入度
四,解题过程
第一博
第二搏
一,题目描述
英文描述
One way to serialize a binary tree is to use preorder 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 "9,3,4,#,#,1,#,#,2,#,6,#,#", where '#' represents a null node.Given a string of comma-separated values preorder, return true if it is a correct preorder traversal serialization of a binary tree.
It is guaranteed that 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".
Note: You are not allowed to reconstruct the tree.
中文描述
序列化二叉树的一种方法是使用前序遍历。当我们遇到一个非空节点时,我们可以记录下这个节点的值。如果它是一个空节点,我们可以使用一个标记值记录,例如 #。
例如,上面的二叉树可以被序列化为字符串 "9,3,4,#,#,1,#,#,2,#,6,#,#",其中 # 代表一个空节点。给定一串以逗号分隔的序列,验证它是否是正确的二叉树的前序序列化。编写一个在不重构树的条件下的可行算法。
每个以逗号分隔的字符或为一个整数或为一个表示 null 指针的 '#' 。
你可以认为输入格式总是有效的,例如它永远不会包含两个连续的逗号,比如 "1,,3" 。
示例与说明
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/verify-preorder-serialization-of-a-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
二,解题思路
1,栈合并节点
算法思路很简单,直接上图:
一个叶子节点+两个空节点 转化为一个空节点,自底向上 ,直到最终化简为一个空节点。
2,计算入度出度和
参考@负雪明烛【 拍案叫绝的两种解法:「栈」和「入度出度」】
三,AC代码
Java
栈
class Solution {public boolean isValidSerialization(String preorder) {String[] tree = preorder.split(",");Deque<String> record = new LinkedList<>();for (int i = 0; i < tree.length; i++) {if (!tree[i].equals("#")) record.push(tree[i]);else {if (!record.isEmpty() && record.peek().equals("#")) {while (!record.isEmpty() && record.peek().equals("#")) {if (record.size() < 2) return false;record.pop();record.pop();}record.push("#");} else record.push(tree[i]);}// System.out.println("----------------");// System.out.println(record.toString());}return record.size() == 1 && record.peek().equals("#");}
}// 另一种更简洁的实现方式
class Solution {public boolean isValidSerialization(String preorder) {LinkedList<String> stack = new LinkedList<>();for (String s : preorder.split(",")) {stack.push(s);while (stack.size() >= 3&& stack.get(0).equals("#")&& stack.get(1).equals("#")&& !stack.get(2).equals("#")) {stack.pop();stack.pop();stack.pop();stack.push("#");}}return stack.size() == 1 && stack.pop().equals("#");}
}
计算出入度
// 错误的实现方法,比如测试用例为"#,7,6,9,#,#,#"
// class Solution {
// public boolean isValidSerialization(String preorder) {
// String[] tree = preorder.split(",");
// int diff = 1;
// for (int i = 0; i < tree.length; i++) {
// if (tree[i].equals("#")) {
// diff -= 1;
// if (diff < 0) return false;
// } else {
// diff += 1;
// }
// System.out.println(diff);
// }
// return diff == 0;
// }
// }class Solution {public boolean isValidSerialization(String preorder) {int diff = 1;for(String s : preorder.split(",")){diff--;// 这种直接先减一后面正常节点加2的方法 可以巧妙处理头节点为空的用例if (diff < 0){return false;}if(!s.equals("#")){diff += 2;}}return diff == 0;}
}
四,解题过程
第一博
老师上课讲的使用栈的方法。细节部分调了很久
第二搏
题解中看到一个更简洁的方法,计算入度、出度。针对本题入度和等于出度和
LeetCode_Stack_331. Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化(Java)【栈,字符串处理】相关推荐
- Leetcode -- Verify Preorder Serialization of a Binary Tree
题目描述 One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null ...
- LeetCode Verify Preorder Serialization of a Binary Tree
问题:给出一个字符串,验证二叉树的前序遍历是否正确. 思路:对于#字符,表示空结点,消耗一个槽位.对于非#字符,消耗一个槽位,因为包含两个子结点,需要新增两个槽位.最后看槽位是否等于0 具体代码参考: ...
- leetcode 331. Verify Preorder Serialization of a Binary Tree | 331. 验证二叉树的前序序列化(栈解法)
题目 https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ 题解 看了答案,参考:拍案叫绝的两种解 ...
- 331. Verify Preorder Serialization of a Binary Tree
二刷 2种做法,indegree outdegree是一种. public class Solution {public boolean isValidSerialization(String pre ...
- [Lintcode]66. Binary Tree Preorder Traversal/[Leetcode]144. Binary Tree Preorder Traversal
66. Binary Tree Preorder Traversal/144. Binary Tree Preorder Traversal 本题难度: Easy/Medium Topic: Bina ...
- 654. Maximum Binary Tree最大二叉树
网址:https://leetcode.com/problems/maximum-binary-tree/ 参考: https://leetcode.com/problems/maximum-bina ...
- Binary Tree Level Order Traversal II leetcode java
题目: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from ...
- [LeetCode][JavaScript]Invert Binary Tree 反转二叉树
反转二叉树 其实我从没有想到前端面试会问到这个问题,题目来源于google的面试 Google: 90% of our engineers use the software you wrote (Ho ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题--居然还是不会么-- /*** Definition for a bi ...
最新文章
- Printed Exception strings - what do all those flags mean?(转)
- Sklearn(v3)——朴素贝叶斯(3)
- Remote Desktop 访问设置
- python画图代码-Python为啥这么牛?一行Python代码除了画图竟然还有这些功能!
- XLNET:换一个思路做预训练,效果杠杠滴
- 简述deque容器的插入删除原理
- 关于AD9371FilterWizard无法运行问题
- matlab 三对角矩阵 追赶法,追赶法求解三对角方程及其算例.doc
- Matter.js 2D 物理引擎试玩报告
- linux 网络问题 ipv4 forwarding
- 一山一世界,雅居乐陈卓林“乐活”美好生活,在此进阶
- 微信小程序接收二进制流文件(图片预览,文件打开)
- 使用DHT11和51单片机进行温湿度的读取(保证好用版本)
- 朴素贝叶斯法的参数估计——贝叶斯估计及其Python实现
- 铁路网络售票是利用计算机,铁路客运计算机售票具体操作图文.pdf
- 【C#】最全单据打印(打印模板、条形码二维码、字体样式、项目源码)
- python卸载与扩展包卸载
- PMP中敏捷知识梳理
- Vue+element ui实现好看的个人中心
- 计算机在工作中的应用,计算机技术在设计工作中的应用