【LeetCode】Verify Preorder Serialization of a Binary Tree(331)
1. 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 #.
_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
2. Answer
public class Solution {
public static boolean isValidSerialization(String preorder) {
String[] strs = preorder.split(",");
int degree = -1; // root has no indegree, for compensate init with -1
for (String str : strs) {
degree++; // all nodes have 1 indegree (root compensated)
if (degree > 0) { // total degree should never exceeds 0
return false;
}
if (!str.equals("#")) {// only non-leaf node has 2 outdegree
degree -= 2;
}
}
return degree == 0;
}
}
【LeetCode】Verify Preorder Serialization of a Binary Tree(331)的更多相关文章
- 【树】Lowest Common Ancestor of a Binary Tree(递归)
题目: Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Accor ...
- 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)
[LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...
- leetcode 331. Verify Preorder Serialization of a Binary Tree
传送门 331. Verify Preorder Serialization of a Binary Tree My Submissions QuestionEditorial Solution To ...
- LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27
331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...
- 【LeetCode】449. Serialize and Deserialize BST 解题报告(Python)
[LeetCode]449. Serialize and Deserialize BST 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/pro ...
- 【LeetCode】779. K-th Symbol in Grammar 解题报告(Python)
[LeetCode]779. K-th Symbol in Grammar 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingz ...
- 【LeetCode】792. Number of Matching Subsequences 解题报告(Python)
[LeetCode]792. Number of Matching Subsequences 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...
- 【LeetCode】881. Boats to Save People 解题报告(Python)
[LeetCode]881. Boats to Save People 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu ...
- 【LeetCode】802. Find Eventual Safe States 解题报告(Python)
[LeetCode]802. Find Eventual Safe States 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemi ...
随机推荐
- #iOS问题记录# 关于UITableViewcel的分割线去掉问题
十分清楚的记得以前在使用的时候,通过[_mTableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];这一句话来达到效果的. 这次怎么 ...
- 浅谈PHP7新特性
1. 运算符(NULL 合并运算符) 把这个放在第一个说是因为我觉得它很有用.用法: $a = $_GET['a'] ?? 1; 它相当于: $a = isset($_GET['a']) ? $_GE ...
- ubuntu自定义分辨率
首先说下为啥要专门敲个文章来说明这个问题,因为我最近入手了一台分辨率为3200*1800的高分辨率笔记本,但使用的时候发现现在的操作系统及其诸多软件对高分辨率屏幕的支持真的是太烂,字体发虚或者变得非常 ...
- 排序合并连接(sort merge join)的原理
排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理 排序合并连接(sort merge join) 访问次数:两张表都只会访 ...
- SQL Server 变更数据捕获(CDC)
标签:SQL SERVER/MSSQL SERVER/数据库/DBA/字段/对象更改 概述 变更数据捕获用于捕获应用到 SQL Server 表中的插入.更新和删除活动,并以易于使用的关系格式提供这些 ...
- AutoMapper 最佳实践
AutoMapper 是一个基于命名约定的对象->对象映射工具. 只要2个对象的属性具有相同名字(或者符合它规定的命名约定),AutoMapper就可以替我们自动在2个对象间进行属性值的映射.如 ...
- C#设计模式之工厂
IronMan之工厂 前言 实用为主,学一些用得到的技术更能在如今的社会里保命. 虽然在日常的工作中设计模式不是经常的用到,但是呢,学习它只有好处没有坏处. 设计模式像是一种“标签”,它是代码编写者思 ...
- Sql Server系列:排序函数
在SQL Server中有4个排序函数:ROW_NUMBER().RANK().DENSE_RANK()及NTILE()函数. 1. ROW_NUMBER()函数 ROW_NUMBER()函数为每条记 ...
- LINQ系列:LINQ to SQL Take/Skip
1. Take var expr = context.Products .Take(); var expr = (from p in context.Products select p) .Take( ...
- Android实现不重复启动APP的方法
转载博客:http://blog.sina.cn/dpool/blog/s/blog_5de73d0b0102vpai.html?utm_source=bshare&utm_campaign= ...