-
Notifications
You must be signed in to change notification settings - Fork 0
/
0257.cpp
120 lines (110 loc) · 2.34 KB
/
0257.cpp
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>
#include <vector>
#include <stack>
#include <string>
using namespace std;
struct TreeNode
{
int val;
TreeNode* left;
TreeNode* right;
TreeNode(int x) :val(x), left(NULL), right(NULL) {}
};
class Solution
{
public:
/*
// 递归遍历 先序 叶子节点
vector<string> binaryTreePaths(TreeNode* root)
{
vector<int> ans;
vector<string> string_ans;
if (root == NULL)
return string_ans;
else
{
preorder(root, ans, string_ans);
return string_ans;
}
}
// 1. 确定参数和返回参数
void preorder(TreeNode* root, vector<int> &ans, vector<string>&string_ans)
{
/// 2. 确定终止条件
ans.push_back(root->val);
// 3. 单次递归处理
// 是叶子结点
if (root->left == NULL && root->right == NULL) // 中
{
string path = "";
for (int i = 0; i < ans.size(); i++)
{
if (i == 0)
{
// to_string 将数字常量转换为字符串
path = path + to_string(ans[i]);
continue;
}
path = path + "->";
path = path + to_string(ans[i]);
}
string_ans.push_back(path);
}
if (root->left != NULL) // 左
{
preorder(root->left, ans, string_ans);
ans.pop_back();
}
if (root->right != NULL) // 右
{
preorder(root->right, ans, string_ans);
ans.pop_back();
}
return;
}
*/
// 非递归 统一迭代法实现先序遍历 中 左 右 不能回溯 好像没法用统一迭代法 用常规方法
vector<string> binaryTreePaths(TreeNode* root)
{
vector<string> string_ans;
if (root == NULL)
return string_ans;
stack<TreeNode*> st;
stack<string> pathSt;
st.push(root);
pathSt.push(to_string(root->val));
while (!st.empty())
{
TreeNode* node = st.top();
st.pop(); // 中
string path = pathSt.top();
pathSt.pop(); // 取出路径
if (node->left == NULL && node->right == NULL)
string_ans.push_back(path);
if (node->right != NULL)
{
st.push(node->right); // 右
pathSt.push(path + "->" + to_string(node->right->val));
}
if (node->left != NULL) // 左
{
st.push(node->left);
pathSt.push(path + "->" + to_string(node->left->val));
}
}
return string_ans;
}
};
int main()
{
TreeNode* root = new TreeNode(1);
TreeNode* node = root;
node->left = new TreeNode(2);
node->right = new TreeNode(3);
node->left->left = new TreeNode(4);
node->left->right = new TreeNode(5);
// node->right->right = new TreeNode(7);
Solution S;
S.binaryTreePaths(root);
return 0;
}