题目描述
原题链接:257. 二叉树的所有路径
解题思路
一、递归法
(1) 先序遍历DFS
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<string> res;
void traversal(string s, TreeNode* root) {
if(!root->left && !root->right) {
s += to_string(root->val); // 对叶子处和非叶子处的字符串相加分别处理
res.push_back(s);
return ;
}
s += to_string(root->val) + "->"; // 非叶子后面带上箭头,叶子后面不带箭头
if(root->left) traversal(s, root->left);
if(root->right) traversal(s, root->right);
}
vector<string> binaryTreePaths(TreeNode* root) {
traversal("", root);
return res;
}
};
时间复杂度
O
(
n
2
)
O(n^2)
O(n2)
空间复杂度
O
(
n
2
)
O(n^2)
O(n2)
(2)标准回溯法形式
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void traversal(TreeNode* root, vector<int>& path, vector<string>& res) {
path.push_back(root->val);
// 遍历到根节点,输出结果
if(!root->left && !root->right) {
string s = "";
for(int i = 0; i < path.size() - 1; i++) {
s += to_string(path[i]);
s += "->";
}
// 左后一个单独相加
s += to_string(path[path.size() - 1]);
res.push_back(s);
return ;
}
// 左子树不为空,遍历,遍历完后回退
if(root->left) {
traversal(root->left, path, res);
path.pop_back();
}
// 左子树不为空,遍历,遍历完后回退
if(root->right) {
traversal(root->right, path, res);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
vector<string> res;
traversal(root, path, res);
return res;
}
};
时间复杂度
O
(
n
2
)
O(n^2)
O(n2)
空间复杂度
O
(
n
2
)
O(n^2)
O(n2)
二、迭代法
(1)先序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
stack<TreeNode*> st;
stack<string> path; // 先序需要使用栈,为保持顺序一致,也用栈维护每条路径上的
st.push(root);
path.push(to_string(root->val));
while(!st.empty()) {
TreeNode* node = st.top(); st.pop();
string s = path.top(); path.pop();
if(!node->left && !node->right) {
res.push_back(s);
}
// 因为栈是先进后出,因此先压入右,再压入左,弹出时候才会是先序遍历
if(node->right) {
st.push(node->left);
path.push(s + "->" + to_string(node->left->val));
}
if(node->left) {
st.push(node->right);
path.push(s + "->" + to_string(node->right->val));
}
}
return res;
}
};
时间复杂度
O
(
n
2
)
O(n^2)
O(n2)
空间复杂度
O
(
n
2
)
O(n^2)
O(n2)
(2)层次遍历BFS
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
queue<string> path; // 层序需要用队列,为保持顺序一致,也用队列存路径
queue<TreeNode*> que;
que.push(root);
path.push(to_string(root->val));
while(!que.empty()) {
TreeNode* node = que.front(); que.pop();
string s = path.front(); path.pop();
if(!node->left && !node->right) {
res.push_back(s);
}
if(node->left) {
que.push(node->left);
path.push(s + "->" + to_string(node->left->val));
}
if(node->right) {
que.push(node->right);
path.push(s + "->" + to_string(node->right->val));
}
}
return res;
}
};
时间复杂度
O
(
n
2
)
O(n^2)
O(n2)
空间复杂度
O
(
n
2
)
O(n^2)
O(n2)
参考文章:二叉树的所有路径、257. 二叉树的所有路径