Skip to content

Commit cbd0fb7

Browse files
committed
day 26
1 parent 0e7ce14 commit cbd0fb7

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Verify Preorder Serialization of a Binary Tree
3+
==============================================
4+
5+
One way to serialize a binary tree is to use preorder 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 '#'.
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.
8+
9+
Given a string of comma-separated values preorder, return true if it is a correct preorder traversal serialization of a binary tree.
10+
11+
It is guaranteed that each comma-separated value in the string must be either an integer or a character '#' representing null pointer.
12+
13+
You may assume that the input format is always valid.
14+
15+
For example, it could never contain two consecutive commas, such as "1,,3".
16+
Note: You are not allowed to reconstruct the tree.
17+
18+
Example 1:
19+
Input: preorder = "9,3,4,#,#,1,#,#,2,#,6,#,#"
20+
Output: true
21+
22+
Example 2:
23+
Input: preorder = "1,#"
24+
Output: false
25+
26+
Example 3:
27+
Input: preorder = "9,#,#,1"
28+
Output: false
29+
30+
Constraints:
31+
1 <= preorder.length <= 104
32+
preorder consist of integers in the range [0, 100] and '#' separated by commas ','.
33+
*/
34+
35+
class Solution {
36+
public:
37+
int j;
38+
vector<string> arr;
39+
40+
bool dfs() {
41+
if(j >= arr.size()) return false;
42+
43+
if(arr[j] == "#"){
44+
j++;
45+
return true;
46+
}
47+
48+
j++;
49+
return dfs() && dfs();
50+
}
51+
52+
bool isValidSerialization(string s) {
53+
string curr = "";
54+
arr = vector<string> (0);
55+
56+
for (int i = 0; i <= s.size(); ++i) {
57+
if(i == s.size() || s[i] == ',') {
58+
arr.push_back(curr);
59+
curr = "";
60+
}
61+
else curr += s[i];
62+
}
63+
64+
j = 0;
65+
return dfs() && j == arr.size();
66+
}
67+
};

Leetcode Daily Challenge/August-2021/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@
2727
| 23. | [Two Sum IV - Input is a BST](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3908/) | [cpp](./23.%20Two%20Sum%20IV%20-%20Input%20is%20a%20BST.cpp) |
2828
| 24. | [Complex Number Multiplication](https://leetcode.com/explore/featured/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3917/) | [cpp](./24.%20Complex%20Number%20Multiplication.cpp) |
2929
| 25. | [Sum of Square Numbers](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3918/) | [cpp](./25.%20Sum%20of%20Square%20Numbers.cpp) |
30+
| 26. | [Verify Preorder Serialization of a Binary Tree](https://leetcode.com/explore/challenge/card/august-leetcoding-challenge-2021/616/week-4-august-22nd-august-28th/3920/) | [cpp](./26.%20Verify%20Preorder%20Serialization%20of%20a%20Binary%20Tree.cpp) |
31+
| 27. | []() | [cpp](./27.%20.cpp) |
32+
| 28. | []() | [cpp](./28.%20.cpp) |

0 commit comments

Comments
 (0)