331. Verify Preorder Serialization of a Binary Tree

原创 2016年05月30日 20:05:36

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

题意:判断一颗树的表达的合法性。

思路:用栈循环相消。

class Solution {
public:
	bool isValidSerialization(string preorder) {
		string s = spilt(preorder, ',');
		while (!s.empty()){
			if (push(s) != 0)
				return false;
			s = spilt(preorder, ',');
		}
		return mystack.size()==1 && mystack.top()=='#';
	}
	
private:
	string spilt(string& preorder, char ch){
		if (preorder.empty())
			return preorder;
		string s;
		int length = preorder.size();
		int i;
		for (i = 0; i < length; i++){
			if (preorder[i] == ch){
				s = preorder.substr(0, i);
				preorder = preorder.substr(i + 1);
				break;
			}
		}
		if (i == length){
			s = preorder;
			preorder.clear();
		}
		return s;
	}
	int push(string s){
		if (s.empty())
			return 1;
		if (s.compare("#") == 0){
			if (mystack.empty()){
				mystack.push('#');
			}
			else{
				if (mystack.top() == '#'){
					mystack.pop();
					if (mystack.empty())
						return 3;
					mystack.pop();
					while (!mystack.empty() && mystack.top() == '#'){
						mystack.pop();
						if (mystack.empty())
						return 4;
						mystack.pop();
					}
					mystack.push('#');
				}
				else{
					mystack.push('#');
				}
			}
		}
		else{
			mystack.push('t');
		}
		return 0;
	}
	stack<char> mystack;
};









版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

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

331. Verify Preorder Serialization of a Binary Tree(C++实现)

算法思想一:(二叉树中所有节点的出度=入度) 根结点有2出度0入度,分支结点有2出度1入度,空结点有0出度1入度。遍历序列,判断出度是否等于入度! 记 K=出度-入度。如果序列化是正确的,那么 K...

190. Reverse Bits\331. Verify Preorder Serialization of a Binary Tree

Reverse Bits description implementation Verify Preorder Serialization of a Binary Tree description i...

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 ...
  • riskys
  • riskys
  • 2017年02月15日 20:49
  • 57

[leetcode-331]Verify Preorder Serialization of a Binary Tree

该题一共有三种解法,首先我们给出字符串分隔的函数,该函数在以下三个方法中均会用到: vector split(string input, char delimiter) { vector r...
  • hpingwu
  • hpingwu
  • 2016年04月05日 18:40
  • 161

331. Verify Preorder Serialization of a Binary Tree【M】【16】【leetcode】

One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node,...
  • sscssz
  • sscssz
  • 2016年02月24日 18:47
  • 437

LeetCode 331. Verify Preorder Serialization of a Binary Tree(校验二叉树的前序遍历序列化)

原题网址:https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ One way to seri...
  • jmspan
  • jmspan
  • 2016年04月28日 07:36
  • 360

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

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 解题报告

题目链接: https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/ One way to ser...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:331. Verify Preorder Serialization of a Binary Tree
举报原因:
原因补充:

(最多只允许输入30个字)