257 二叉树的所有路径

本文最后更新于:2022年9月12日 晚上

给定一个二叉树,返回所有从根节点到叶子节点的路径。

说明: 叶子节点是指没有子节点的节点。

示例:

1
2
3
4
5
6
7
8
9
10
11
输入:

1
/ \
2 3
\
5

输出: ["1->2->5", "1->3"]

解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3

Solution

参考 代码随想录 ,分析回溯过程

  • 递归法,前序遍历
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
class Solution {
private:

void traversal(TreeNode* cur, vector<int>& path, vector<string>& result) {
path.push_back(cur->val);
// 这才到了叶子节点
if (cur->left == NULL && cur->right == NULL) {
string sPath;
for (int i = 0; i < path.size() - 1; i++) {
sPath += to_string(path[i]);
sPath += "->";
}
sPath += to_string(path[path.size() - 1]);
result.push_back(sPath);
return;
}
if (cur->left) {
traversal(cur->left, path, result);
path.pop_back(); // 回溯
}
if (cur->right) {
traversal(cur->right, path, result);
path.pop_back(); // 回溯
}
}

public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
vector<int> path;
if (root == NULL) return result;
traversal(root, path, result);
return result;
}
};
  • 代码精简
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
// @lc code=start
/**
* 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;
if(root==nullptr) return res;
if(root->left==nullptr && root->right==nullptr){
res.push_back(to_string(root->val));
return res;
}
vector<string> leftPaths = binaryTreePaths(root->left);
for(int i=0; i<leftPaths.size(); ++i){
res.push_back(to_string(root->val)+"->"+leftPaths[i]);
}
vector<string> rightPaths = binaryTreePaths(root->right);
for(int i=0; i<rightPaths.size(); ++i){
res.push_back(to_string(root->val)+"->"+rightPaths[i]);
}
return res;
}
};
// @lc code=end
  • 迭代法,广度优先搜索 参考**@LeetCode官方**
  • 维护两个队列,存储节点和根到该节点的路径。

img

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
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if(root==nullptr) return res;
queue<TreeNode *> qNode;
queue<string> qPath;

qNode.push(root);
qPath.push(to_string(root->val));

while(!qNode.empty()){
TreeNode* node = qNode.front();
string path = qPath.front();
qNode.pop(); qPath.pop();

if(node->left==nullptr && node->right==nullptr){
res.push_back(path);
} else {
if(node->left){
qNode.push(node->left);
qPath.push(path+"->"+to_string(node->left->val));
}
if(node->right){
qNode.push(node->right);
qPath.push(path+"->"+to_string(node->right->val));
}
}
}

return res;
}
};

java

  • 递归法
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
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if (root == null) {
return result;
}
List<Integer> path = new ArrayList<>();
traversal(root, path, result);
return result;
}

private void traversal(TreeNode cur, List<Integer> path, List<String> result) {
path.add(cur.val);
if (cur.left == null && cur.right == null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < path.size() - 1; ++i) {
sb.append(path.get(i)).append("->");
}
sb.append(path.get(path.size() - 1));
result.add(sb.toString());
}

if (cur.left != null) {
traversal(cur.left, path, result);
path.remove(path.size() - 1);
}
if (cur.right != null) {
traversal(cur.right, path, result);
path.remove(path.size() - 1);
}
}
}
  • 迭代法,层序遍历
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
class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> result = new ArrayList<>();
if (root == null) {
return result;
}
Queue<TreeNode> nodeQueue = new LinkedList<>();
Queue<String> pathQueue = new LinkedList<>();
nodeQueue.offer(root);
pathQueue.offer(Integer.toString(root.val));

while (!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.poll();
String path = pathQueue.poll();
if (node.left == null && node.right == null) {
result.add(path);
} else {
if (node.left != null) {
nodeQueue.offer(node.left);
pathQueue.offer(new StringBuffer(path).append("->")
.append(node.left.val).toString());
}
if (node.right != null) {
nodeQueue.offer(node.right);
pathQueue.offer(new StringBuffer(path).append("->")
.append(node.right.val).toString());
}
}
}
return result;
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!