[LeetCode]331. Verify Preorder Serialization of a Binary Tree
来源:互联网 发布:python检测ip能否ping 编辑:程序博客网 时间:2024/05/01 07:43
Problem Description
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 #.
[https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/]
思路
没啥好说的,遇到俩#就弹出俩然后把栈顶的数换成#,如果栈空就说明是对的
Code
package q331;import java.util.Stack;public class Solution { public boolean isValidSerialization(String preorder) { Stack<String> s = new Stack<String>(); if (preorder.length() <= 1 && !preorder.equals("#")) return false; if (preorder.charAt(0) == '#' && preorder.length() > 1) return false; String b; String[] str = preorder.split(","); s.push(str[0]); for (int i = 1; i < str.length; i++) { b = str[i]; while (s.peek().equals("#") && b.equals("#")) { s.pop(); if (s.isEmpty()) { if (i != str.length - 1) return false; break; } if (!s.peek().equals("#")) { s.pop(); if (s.isEmpty()) { if (i != str.length - 1) return false; break; } b = s.peek(); s.push("#"); } } if (b.equals(str[i])) s.push(b); } while (!s.isEmpty() && s.peek().equals("#")) s.pop(); return s.isEmpty(); }// public static void main(String[] args) {// Solution s = new Solution();// // System.out.println(s.isValidSerialization("9,3,4,#,#,1,#,#,2,#,6,#,#"));// // System.out.println(s.isValidSerialization("9,#,#,1"));// System.out.println(s.isValidSerialization("9,#,92,#,#"));// }}
0 0
- leetcode 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
- LeetCode 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
- [leetcode] 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
- LeetCode 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
- 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 Verify Preorder Serialization of a Binary Tree
- [leetcode] Verify Preorder Serialization of a Binary Tree
- [Leetcode]Verify Preorder Serialization of a Binary Tree
- Delphi判断是否有全屏程序
- 【android】LayoutInflater 的 inflater 方法浅析
- MyEclipse快捷键大全
- MPICH Demo测试__两主机进行通信出现的问题及解决方案
- windows下 VIM安装配置,打造PHP IDE开发工具
- [LeetCode]331. Verify Preorder Serialization of a Binary Tree
- c语言技巧
- ios开发——日常之XCode 文件后面带有问号的问题怎么解决??
- 46腾讯研发工程师笔试卷
- 基于数据库、php、ajax的 3级地区联动
- 管家病毒查杀模块逆向分析
- Maven 安装配置
- XManage-xftp上传到linux
- Struts的类型转换及输入校验(Struts2的输入校验)