331. Verify Preorder Serialization of a Binary Tree
来源:互联网 发布:com域名删除的准确时间 编辑:程序博客网 时间:2024/05/16 06:22
用# 代表树的空节点 判断是否是有效的前序遍历树序列
split() 用单引号竟然不对!! equals() 用单引号竟然不对!!!
public class Solution { public boolean isValidSerialization(String preorder) { String[] pre=preorder.split(","); int diff=1; for(String str:pre){ if(--diff<0) return false; if(!str.equals("#")) diff+=2; } return diff==0; }}后序遍历
public boolean isValidSerialization_PostOrder(String postorder) {String[] nodes = postorder.split(",");int diff = 1;for (String node : nodes) {diff--;if (!node.equals("#")) diff += 2;// Post-Order traversal fail criteriaif (diff > 0) return false;}return diff == 0;}
中序遍历public boolean isValidSerialization_InOrder(String inorder) {String[] nodes = inorder.split(",");int diff = 1;for (String node : nodes) {diff--;if (!node.equals("#")) diff += 2;// In-Order traversal fail criteriaif (diff > 1) return false;}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
- Example of Reveal animations on Android 5.0
- export,echo
- 将mysql中查询出来的数据 序列化到protobuffer消息结构体中
- 第一次blog记
- 1039. Course List for Student (25)
- 331. Verify Preorder Serialization of a Binary Tree
- 排序算法(二)——冒泡排序法
- 设计模式-工厂方法模式
- UGUI内核大探究(十一)ScrollRect与ScrollBar
- HDU 5878 I Count Two Three (暴力) 2016 ACM/ICPC Asia Regional Qingdao Online
- MATLAB小记- polybool与polyarea
- 信息技术从业者的事业、实践、金句代表
- 链表中倒数第k个结点
- Elasticsearch(6)实践二-检索一个文档