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

Appearance settings

Latest commit

 

History

History
History
35 lines (32 loc) · 832 Bytes

File metadata and controls

35 lines (32 loc) · 832 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
33
34
35
//
// Solution144.cpp
// Algorithm
//
// Created by Pancf on 2019/12/21.
// Copyright © 2019 Pancf. All rights reserved.
//
#include "Solution144.hpp"
#include <stack>
using std::stack;
vector<int> Solution144::preorderTraversal(TreeNode *root) {
vector<int> rv;
if (!root) {
return rv;
}
auto cur = root;
stack<TreeNode *> st;
while (cur || !st.empty()) {
if (cur) {
//前序遍历则要保存当前结点的值,然后将结点压栈,向左走一步
rv.push_back(cur->val);
st.push(cur);
cur = cur->left;
} else {
//栈顶结点的左子树为空时,出栈,然后访问出栈结点的右子树
cur = st.top();
st.pop();
cur = cur->right;
}
}
return rv;
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.