Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit86f9497

Browse files
committed
New Problem Solution "Verify Preorder Serialization of a Binary Tree"
1 parent7fc247a commit86f9497

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ LeetCode
5454
|338|[Counting Bits](https://leetcode.com/problems/counting-bits/)|[C++](./algorithms/cpp/countingBits/CountingBits.cpp)|Medium|
5555
|337|[House Robber III](https://leetcode.com/problems/house-robber-iii/)|[C++](./algorithms/cpp/houseRobber/houseRobberIII.cpp)|Medium|
5656
|334|[Increasing Triplet Subsequence](https://leetcode.com/problems/increasing-triplet-subsequence/)|[C++](./algorithms/cpp/increasingTripletSubsequence/increasingTripletSubsequence.cpp)|Medium|
57+
|331|[Verify Preorder Serialization of a Binary Tree](https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/)|[C++](./algorithms/cpp/verifyPreorderSerializationOfABinaryTree/VerifyPreorderSerializationOfABinaryTree.cpp)|Medium|
5758
|330|[Patching Array](https://leetcode.com/problems/patching-array/)|[C++](./algorithms/cpp/patchingArray/PatchingArray.cpp)|Medium|
5859
|329|[Longest Increasing Path in a Matrix](https://leetcode.com/problems/longest-increasing-path-in-a-matrix/)|[C++](./algorithms/cpp/longestIncreasingPathInAMatrix/LongestIncreasingPathInAMatrix.cpp)|Medium|
5960
|328|[Odd Even Linked List](https://leetcode.com/problems/odd-even-linked-list/)|[C++](./algorithms/cpp/oddEvenLinkedList/OddEvenLinkedList.cpp)|Easy|
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Source : https://leetcode.com/problems/verify-preorder-serialization-of-a-binary-tree/
2+
// Author : Hao Chen
3+
// Date : 2017-01-06
4+
5+
/***************************************************************************************
6+
*
7+
* One way to serialize a binary tree is to use pre-order traversal. When we encounter
8+
* a non-null node, we record the node's value. If it is a null node, we record using a
9+
* sentinel value such as #.
10+
*
11+
* _9_
12+
* / \
13+
* 3 2
14+
* / \ / \
15+
* 4 1 # 6
16+
* / \ / \ / \
17+
* # # # # # #
18+
*
19+
* For example, the above binary tree can be serialized to the string
20+
* "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node.
21+
*
22+
* Given a string of comma separated values, verify whether it is a correct preorder
23+
* traversal serialization of a binary tree. Find an algorithm without reconstructing
24+
* the tree.
25+
*
26+
* Each comma separated value in the string must be either an integer or a character
27+
* '#' representing null pointer.
28+
*
29+
* You may assume that the input format is always valid, for example it could never
30+
* contain two consecutive commas such as "1,,3".
31+
*
32+
* Example 1:
33+
* "9,3,4,#,#,1,#,#,2,#,6,#,#"
34+
* Return true
35+
* Example 2:
36+
* "1,#"
37+
* Return false
38+
* Example 3:
39+
* "9,#,#,1"
40+
* Return false
41+
*
42+
* Credits:Special thanks to @dietpepsi for adding this problem and creating all test
43+
* cases.
44+
***************************************************************************************/
45+
46+
classSolution {
47+
public:
48+
49+
// we know the following facts:
50+
// 1) if we met a non-null node, then this node will generate two child node.
51+
// 2) if we met a null node, then this node will generate zero child node.
52+
//
53+
// so the idea is,
54+
// 1) we can have a counter to calculate how many node we are going to expect
55+
// 2) once we have the expected node, then we can decrease the counter.
56+
// 3) finally, we will check the counter is zero or not.
57+
//
58+
// the algorithm as below:
59+
// 1) when we meet a non-null node, just simply do `counter++`. because:
60+
// 1.1) we will expect 2 more node after, then we do `counter += 2`.
61+
// 1.2) but the current node also meet the expection of parent node , so, it need remove 1 in counter.
62+
// finally, the `counter = counbter + 2 -1`
63+
// 2) when we meet a null node, just simply do `counter--`.
64+
65+
boolisValidSerialization(string preorder) {
66+
vector<string> list;
67+
split(preorder,',', list);
68+
//we initailize the counter as 1,
69+
//because we expect at lease 1 node in the tree.
70+
int node_expected =1;
71+
for (auto node : list) {
72+
if (node_expected ==0)returnfalse;
73+
node =="#" ? node_expected-- : node_expected++;
74+
}
75+
return node_expected ==0;
76+
}
77+
78+
voidsplit(const string &s,char delim, vector<string> &elems) {
79+
stringstreamss(s);
80+
string item;
81+
while (getline(ss, item, delim)) {
82+
elems.push_back(item);
83+
}
84+
}
85+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp