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 #.node
_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.git
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.github
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".app
Example 1:ui
Input: "9,3,4,#,#,1,#,#,2,#,6,#,#"
Output: true
Example 2:指针
Input: "1,#"
Output: false
Example 3:code
Input: "9,#,#,1"
Output: falseorm
序列化二叉树的一种方法是使用前序遍历。当咱们遇到一个非空节点时,咱们能够记录下这个节点的值。若是它是一个空节点,咱们能够使用一个标记值记录,例如 #。ip
_9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # #
例如,上面的二叉树能够被序列化为字符串 "9,3,4,#,#,1,#,#,2,#,6,#,#",其中 # 表明一个空节点。
给定一串以逗号分隔的序列,验证它是不是正确的二叉树的前序序列化。编写一个在不重构树的条件下的可行算法。
每一个以逗号分隔的字符或为一个整数或为一个表示 null 指针的 '#' 。
你能够认为输入格式老是有效的,例如它永远不会包含两个连续的逗号,好比 "1,,3" 。
示例 1:
输入: "9,3,4,#,#,1,#,#,2,#,6,#,#"
输出: true
示例 2:
输入: "1,#"
输出: false
示例 3:
输入: "9,#,#,1"
输出: false
# -*- coding: utf-8 -*- # @Author: 何睿 # @Create Date: 2019-03-17 09:44:27 # @Last Modified by: 何睿 # @Last Modified time: 2019-03-17 10:03:55 class Solution: def isValidSerialization(self, preorder: str) -> bool: # stack:用于记录遍历到的节点值 # count:stack 中剩余的节点个数 stack, count = [], 0 for item in preorder.split(","): stack.append(item) count += 1 # 若是 stack 中末位两个元素是 #,说明这两个节点前面是一个叶子节点 # 将两个 # 弹出 ,将叶子节点置为 None,即 # # 若是是前序遍历,那么 stack 最后必定会剩下一个 # while count > 1 and stack[-1] == "#" and stack[-2] == "#": stack.pop() stack.pop() if not stack: return False stack[-1] = "#" count -= 2 # 当且仅当 stack 中只剩下一个元素且为 # 时返回 True. return True if len(stack) == 1 and stack[0] == "#" else False