1、题目
2、解决方案
1 | /** |
前序遍历
可以很容易找到根节点,然后在中序排列
中查找这个根节点的索引位置;
这个位置把中序排序
分为了左右部分的树,然后递归遍历即可;
需要注意的是一开始我利用
1 | vector<int>::iterator root = find(ibegin, iend, *pbegin); |
来查找索引,很耗时间,每次都要找一次;
然后看了一下提交的答案,可以使用一个map
,利用结点作为key
然后索引作为value
,可以考虑为在程序开始前提前把数据读到缓存中,后续可以很快的获取索引,对于需要频繁查找的程序很有用;
但是内存消耗感觉有点大,毕竟把数据重新复制了一遍,不过这种利用map/array,消耗一部分内存以大幅度提高时间的思想值得学习。
3、讲讲map
http://www.cplusplus.com/reference/map/map/
3.1 Overview and feature
std::map
是一种数据关联的容器,存储按特定顺序由键(key)和值(value)的组合形成的元素。在map中,key通常用于对元素进行排序和唯一标识,而value存储与该key关联的内容。
map通常比unordered_map通过key访问value要慢,但是map允许基于map的顺序直接对子集进行迭代。
可以使用方括号运算符((operator [])通过对应的键直接访问map中的映射值。
map通常实现为二进制搜索树。
3.2、 Member functions
-
Construct map (public member function )
-
Map destructor (public member function )
-
Copy container content (public member function )
Iterators:
-
Return iterator to beginning (public member function )
-
Return iterator to end (public member function )
-
Return reverse iterator to reverse beginning (public member function )
-
Return reverse iterator to reverse end (public member function )
-
Return const_iterator to beginning (public member function )
-
Return const_iterator to end (public member function )
-
Return const_reverse_iterator to reverse beginning (public member function )
-
Return const_reverse_iterator to reverse end (public member function )
Capacity:
-
Test whether container is empty (public member function )
-
Return container size (public member function )
-
Return maximum size (public member function )
Element access:
-
Access element (public member function )
-
Access element (public member function )
Modifiers:
-
Insert elements (public member function )
-
Erase elements (public member function )
-
Swap content (public member function )
-
Clear content (public member function )
-
Construct and insert element (public member function )
-
Construct and insert element with hint (public member function )
Observers:
-
Return key comparison object (public member function )
-
Return value comparison object (public member function )
Operations:
-
Get iterator to element (public member function )
-
Count elements with a specific key (public member function )
-
Return iterator to lower bound (public member function )
-
Return iterator to upper bound (public member function )
-
Get range of equal elements (public member function )
Allocator:
-
Get allocator (public member function )
4、讲讲stack
http://www.cplusplus.com/reference/stack/stack/
4.1 Overview and feature
基础容器可以是任何标准容器类模板或某些其他专门设计的容器类。容器应支持以下操作:
- empty
- size
- back
- push_back
- pop_back
标准容器类vector,deque和list满足这些要求。默认情况下,如果没有为特定的stack类实例指定容器类,则使用标准容器deque。
4.2、 Member functions
-
Construct stack (public member function )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32// constructing stacks
int main ()
{
std::deque<int> mydeque (3,100); // deque with 3 elements
std::vector<int> myvector (2,200); // vector with 2 elements
std::stack<int> first; // empty stack
std::stack<int> second (mydeque); // stack initialized to copy of deque
std::stack<int,std::vector<int> > third; // empty stack using vector
std::stack<int,std::vector<int> > fourth (myvector);
std::cout << "size of first: " << first.size() << '\n';
std::cout << "size of second: " << second.size() << '\n';
std::cout << "size of third: " << third.size() << '\n';
std::cout << "size of fourth: " << fourth.size() << '\n';
return 0;
}
/*
Output:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2
*/
-
Test whether container is empty (public member function )
-
Return size (public member function )
-
Access next element (public member function )
-
Insert element (public member function )
-
Construct and insert element (public member function )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29// stack::emplace
int main ()
{
std::stack<std::string> mystack;
mystack.emplace ("First sentence");
mystack.emplace ("Second sentence");
std::cout << "mystack contains:\n";
while (!mystack.empty())
{
std::cout << mystack.top() << '\n';
mystack.pop();
}
return 0;
}
/*
Output:
mystack contains:
Second sentence
First sentence
*/
Non-member function overloads
-
Relational operators for stack (function )
-
Exchange contents of stacks (public member function )
Non-member class specializations
-
Uses allocator for stack (class template )