LeetCode Verify Preorder Serialization of a Binary Tree
原题链接在这里:https://leetcode.com/problems/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 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
题解:
类似Serialize and Deserialize Binary Tree.
Method 1计算indegree和outdegree是否相加为0, 没遇到一个string, 就是遇到了一个节点, indegree++. root没有indegree, 但会算一遍outdegree, 为了补偿root. inDegree初始化为-1.
每当遇到一个非叶子节点, 就说明它必须有两个孩子. 就是有两个outDegree, 对应的inDegree就减掉2.
最后看inDegree是否为0.
Time Complexity: O(n). Space: O(n).
Method 2 利用stack来表示层级. 两种情况,一是遇到数字, push into stack.
一是#, 看栈顶是不是#, 若是,就一直pop, 每次pop不来两个, 直到不再是#,最后把当前的#再压入stack; 若不是#就 push into stack.
举例
_1_
/ \
3 2
/ \ / \
# # # #
压栈1, 压栈3, 压栈3的左侧叶子节点#. 当遇到3的右侧叶子节点时, pop出来两个, stack顶部变成1, 再把当前叶子节点压入stack中,stack现在有1, #.
就相当于
_1_
/ \
# 2
/ \
# #
以此类推,看最后stack的size是不是1, 并且唯一的保留就是#.
Time Complexity: O(n). Space: O(n). String array 大小 O(n), stack 大小O(logn).
AC Java:
public class Solution {
public boolean isValidSerialization(String preorder) {
if(preorder == null || preorder.length() == 0){
return true;
}
//Method 1, 算indegree 和 outdegree是否相加为0
String [] strArr = preorder.split(",");
int inDegree = -1; //根节点没有indegree, 但下面又算了一遍outdegree, 为了补偿初始inDegree 为-1
for(String str : strArr){
inDegree++; //没遇到一个str, 说明有一个node, 那么久有一个indegree
if(inDegree > 0){
return false;
}
if(!str.equals("#")){ //但凡非叶子节点,都有two children, outdegree就会多两个, 对比indegree就少两个.
inDegree-=2;
}
}
return inDegree == 0;
/*
//Method 2, 利用stack
Stack<String> stk = new Stack<String>();
String [] strArr = preorder.split(",");
for(String str : strArr){
//若此时stack顶是#, 说明该返回上一层, pop两次, 若顶部又是#, 说明再返回一层, 又pop两次
while(str.equals("#") && !stk.isEmpty() && stk.peek().equals("#")){
stk.pop();
if(stk.isEmpty()){ //pop了一个#, stack就空了, 说明没有parent节点, return false
return false;
}
stk.pop(); //一次pop出来两个
}
stk.push(str); //不论当前的str是什么,最后都压入stack中
}
return stk.size() == 1 && stk.peek().equals("#");
*/
}
}
LeetCode Verify Preorder Serialization of a Binary Tree的更多相关文章
- [LeetCode] Verify Preorder Serialization of a Binary Tree 验证二叉树的先序序列化
One way to serialize a binary tree is to use pre-oder traversal. When we encounter a non-null node, ...
- 【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】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 ...
- LeetCode OJ 331. 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 node, ...
- Verify Preorder Serialization of a Binary Tree -- LeetCode
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- 【leetcode】331. 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 ...
- 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 node, ...
随机推荐
- Unity 状态转化机器
using System; using System.Collections; using System.Collections.Generic; using UnityEngine; /** 有限状 ...
- BZOJ3551 : [ONTAK2010]Peaks加强版
首先强制在线的话,肯定是不能再离线排序+平衡树启发式合并了. 这回要用的是线段树合并,每次把两棵线段树合并,总复杂度为$O(n\log n)$ 预处理: 把边按权值从小到大排序,依次加边, 对于边(x ...
- Activiti工作流学习(一)部署对象和流程定义
一.前言 前一段时间在工作中,使用了流程审批,对api的调用非常不熟悉,都是调用别人写好的接口在界面上进行显示,基本了解了流程审批的主要步骤,现对流程审批进行学习,主要是调用api进行CRUD操作,感 ...
- HTML-DOM零碎
以下是取得集合,取得单个元素,取消红色"s" Document.getElementsByName("some") //IE无效Document.getElem ...
- 【noiOJ】p1794
t1794:集合加法 查看 提交 统计 提问 总时间限制: 3000ms 内存限制: 65536kB 描述 给出2个正整数集合A = {pi | 1 <= i <= a},B = {q ...
- 【JAVA】Spring 数据源配置整理
在Spring中,不但可以通过JNDI获取应用服务器的数据源,也可以直接在Spring容器中配置数据源,此外,还可以通过代码的方式创建一个数据源,以便进行无依赖的单元测试. 配置数据源 ...
- PHP.ini文件读取不到
Configuration File (php.ini) Path /usr/local/php/lib Loaded Configuration File (none) Linux 把 dtruss ...
- javaweb之框架标签(day1 框架标签的使用)
框架标签 <frameset> --rows:按照行进行划分<frameset rows='80,*'> --rows:按照列进行划分<frameset cols='80 ...
- iOS中JSONModel的使用
iOS中JSONModel的使用 流弊的JSON数据模型框架 https://github.com/jsonmodel/jsonmodel 版本 1.3.0 如果你喜欢JSONModel,并且使用 ...
- Search Ads 已经在美国区上线 - iOS 移动开发周报(46)
Search Ads 已经在美国区上线 - iOS 移动开发周报(46) 新闻 Search Ads 上线 苹果的 AppStore 搜索广告终于 正式上线了 https://developer.ap ...