Skip to content

Commit 520d2bc

Browse files
authored
Create tree.cpp
1 parent 09076ac commit 520d2bc

File tree

1 file changed

+23
-0
lines changed
  • 0____剑指offer/tree/33.search_tree

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution
2+
{
3+
public:
4+
bool verifyPostorder(vector<int>& postorder, int start, int end)
5+
{
6+
if (start >= end)
7+
return true;
8+
int root = postorder[end];
9+
int index = end - 1;
10+
for (;index >= start;index--)
11+
if (postorder[index] < root)
12+
break;
13+
int less = index;
14+
while (less >= start)
15+
if (postorder[less--] >= root)
16+
return false;
17+
return verifyPostorder(postorder, start, index) && verifyPostorder(postorder, index + 1, end - 1);
18+
}
19+
bool verifyPostorder(vector<int>& postorder)
20+
{
21+
return verifyPostorder(postorder, 0, postorder.size() - 1);
22+
}
23+
};

0 commit comments

Comments
 (0)