<p>One way to serialize a binary tree is to use <strong>preorder traversal</strong>. When we encounter a non-null node, we record the node&#39;s value. If it is a null node, we record using a sentinel value such as <code>&#39;#&#39;</code>.</p>
<img alt="" src="https://assets.leetcode.com/uploads/2021/03/12/pre-tree.jpg" style="width: 362px; height: 293px;" />
<p>For example, the above binary tree can be serialized to the string <code>&quot;9,3,4,#,#,1,#,#,2,#,6,#,#&quot;</code>, where <code>&#39;#&#39;</code> represents a null node.</p>

<p>Given a string of comma-separated values <code>preorder</code>, return <code>true</code> if it is a correct preorder traversal serialization of a binary tree.</p>

<p>It is <strong>guaranteed</strong> that each comma-separated value in the string must be either an integer or a character <code>&#39;#&#39;</code> representing null pointer.</p>

<p>You may assume that the input format is always valid.</p>

<ul>
	<li>For example, it could never contain two consecutive commas, such as <code>&quot;1,,3&quot;</code>.</li>
</ul>

<p><strong>Note:&nbsp;</strong>You are not allowed to reconstruct the tree.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> preorder = "9,3,4,#,#,1,#,#,2,#,6,#,#"
<strong>Output:</strong> true
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> preorder = "1,#"
<strong>Output:</strong> false
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> preorder = "9,#,#,1"
<strong>Output:</strong> false
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= preorder.length &lt;= 10<sup>4</sup></code></li>
	<li><code>preorder</code> consist of integers in the range <code>[0, 100]</code> and <code>&#39;#&#39;</code> separated by commas <code>&#39;,&#39;</code>.</li>
</ul>

class Solution {
public:
TreeNode* makeTree(vector<int>& val)
{
	if (val.empty())
		return nullptr;
	if (val[0] == -1)
	{
		TreeNode* root = new TreeNode(val[0]);
		val.erase(val.begin());
		return root;
	}
	TreeNode* root = new TreeNode(val[0]);
	val.erase(val.begin());
	root->left = makeTree(val);
	root->right = makeTree(val);
	return root;
}

void isValidTree(TreeNode* root, int& size, bool& check)
{
	if (!root) return;
	if (root->val == -1)
	{
		++size;
		return;
	}
	if (!root->left && !root->right)
	{
		check = false;
		return;
	}
	if (root->left)
	{
		if ((root->left->val == -1 && !root->right) || (root->left->val != -1 && !root->right))
		{
			check = false;
			return;
		}
	}
	if (root->right)
	{
		if ((root->right->val == -1 && !root->left) || (root->right->val != -1 && !root->left))
		{
			check = false;
			return;
		}
	}
	++size;
	isValidTree(root->left, size, check);
	isValidTree(root->right, size, check);
}

bool isValidSerialization(string preorder)
{
	vector<int> val;
	string num;
	preorder += ',';
	for (int i = 0; i < preorder.size(); ++i)
	{
		if (preorder[i] == ',') continue;
		if (preorder[i] == '#')
		{
			val.push_back(-1);
			continue;
		}
		if (isdigit(preorder[i]) && preorder[i + 1] == ',')
		{
			num += preorder[i];
			val.push_back(atoi(num.c_str()));
			num.clear();
			continue;
		}
		num += preorder[i];
	}
	vector<int> temp = val;
	TreeNode* root = makeTree(val);
	bool check = true;
	int size = 0;
	isValidTree(root, size, check);
	if (size < temp.size()) return false;
	return check;
}
};
15900