传送门

331. Verify Preorder Serialization of a Binary Tree

My Submissions

QuestionEditorial Solution
Total Accepted: 10790 Total Submissions: 34071 Difficulty: Medium

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 #.

  1. _9_
  2. / \
  3. 3 2
  4. / \ / \
  5. 4 1 # 6
  6. / \ / \ / \
  7. # # # # # #

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

 
题意:
判断给的字符串 是不是一个合法的 前序遍历
 
思路:
用栈,如果 一个节点的 左子树 和 右子树 都 true 的话,该节点 向上返回true
 
false的情况:
1. 某个节点已经 左子树 和 右子树 都遍历过了,后续又 涉及到该节点的儿子(如 1,#,#,#)
2. 根节点是 已经 访问结束,后续还有其它节点 (#,1  或 1,#,#,1)
 
150 / 150 test cases passed.
Status:

Accepted

Runtime: 8 ms
 
 
  1. class Solution {
  2. public:
  3. bool isValidSerialization(string preorder) {
  4. int l = preorder.length();
  5. if(preorder[] == '#'){
  6. if(l == ) return true;
  7. else return false;
  8. }
  9. int i = ;
  10. stack<int> s;
  11. s.push();
  12. while(i<l && preorder[i] != ',') i++;
  13. i++;
  14. int te;
  15. while(i < l)
  16. {
  17. if(preorder[i] == '#'){
  18. if(s.empty() == ) return false;
  19. while(!s.empty())
  20. {
  21. te = s.top();
  22. if(te == ) return false;
  23. s.pop();
  24. s.push(te + );
  25. te = s.top();
  26. if(te == ){
  27. break;
  28. }
  29. if(te == ){
  30. s.pop();
  31. }
  32. }
  33. if(s.empty() == && i < l - ) return false;
  34. }
  35. else{
  36. s.push();
  37. }
  38. while(i < l && preorder[i] != ',') i++;
  39. i++;
  40. }
  41. if(s.size() == ) return true;
  42. return false;
  43. }
  44. };

leetcode 331. Verify Preorder Serialization of a Binary Tree的更多相关文章

  1. 【LeetCode】331. Verify Preorder Serialization of a Binary Tree 解题报告(Python)

    [LeetCode]331. Verify Preorder Serialization of a Binary Tree 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  2. 【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 ...

  3. 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, ...

  4. [LeetCode] 331. Verify Preorder Serialization of a Binary Tree_Medium tag: stack

    One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...

  5. 【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 ...

  6. 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, ...

  7. 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, ...

  8. 331 Verify Preorder Serialization of a Binary Tree 验证二叉树的前序序列化

    序列化二叉树的一种方法是使用前序遍历.当我们遇到一个非空节点时,我们可以记录这个节点的值.如果它是一个空节点,我们可以使用一个标记值,例如 #.     _9_    /   \   3     2  ...

  9. LeetCode 331. 验证二叉树的前序序列化(Verify Preorder Serialization of a Binary Tree) 27

    331. 验证二叉树的前序序列化 331. Verify Preorder Serialization of a Binary Tree 题目描述 每日一算法2019/5/30Day 27LeetCo ...

随机推荐

  1. UVa 12219 Common Subexpression Elimination (stl,模拟,实现)

    一般来说,把一颗子树离散成一个int,把一个结点的字符离散成一个int会方便处理 直接map离散.当然一个结点最多只有4个小写字母,也可以直接编码成一个27进制的整数,舍掉0,为了区分0和0000. ...

  2. CF-1099 D. Sum in the tree

    CF-1099 D. Sum in the tree 题意:结点序号为 1~n 的一个有根树,根序号为1,每个点有一个权值a[i], 然后定义一s[i]表示从根节点到 结点序号为i的结点的路途上所经过 ...

  3. 【分块】[HNOI2010]弹飞绵羊&分块大法祭

    分块(似乎还有一种动态树(LCT)做法) 第一次学习分块,似乎有点小激动 这是黄学长的分块入门博客「分块」数列分块入门1 – 9 by hzwer 题目描述 某天,Lostmonkey发明了一种超级弹 ...

  4. 哪些 Python 库让你相见恨晚?

    知乎用户,A European Swallow. 苇叶.Aran He.jerry等人赞同 补充三个有助于自动化日常工作的: sh:sh 1.08 — sh v1.08 documentation可以 ...

  5. 文件操作-mkdir

    Linux mkdir命令 主要用来创建目录,也可以直接创建多层目录,本文就为大家介绍下 Linux mkdir命令 . 转载自https://www.linuxdaxue.com/linux-com ...

  6. perl学习之:匹配修饰符/s /m

    m 是将字符串作为多行处理,s是将字符串作为单行处理,如果是s在字符串中出现的\n就相当于普通字符. 6.6. Matching Within Multiple Lines6.6.1. Problem ...

  7. centos7.2 安装nginx

    一.首先检查gcc编译器安装了没 二 首先安装必要的库(nginx 中gzip模块需要 zlib 库,rewrite模块需要 pcre 库,ssl 功能需要openssl库),检查一下是否已经安装了( ...

  8. SQL server的GO用法--巨坑

    SQL脚本是一种用SQL语言写的批处理文件(.sql),SQL脚本通常可以由SQL查询分析器来执行. ================================================= ...

  9. 入门人工智能的首选语言为什么会是Python?

    为何人工智能(AI)首选Python?当你读完这篇文章就会明白了.为何人工智能(AI)首选Python?读完这篇文章你就知道了.我们看谷歌的TensorFlow基本上所有的代码都是C++和Python ...

  10. html,css样式错误总结

    a元素不能嵌套a元素 a元素嵌套a元素会使a元素闭合出现混乱,导致浏览器识别出多个a元素.