1 Star 0 Fork 0

Neo_1001/LeetCode刷题记录

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Please pay attention to the specific project description and its upstream code dependency when using it.
Clone or Download
LC_257.java 1.09 KB
Copy Edit Raw Blame History
Neo_1001 authored 2024-04-10 22:16 . first commit
package LeetCode;
import java.util.ArrayList;
import java.util.List;
public class LC_257 {
public static void main(String[] args) {
}
public static List<String> binaryTreePaths(TreeNode root) {
ArrayList<String> res = new ArrayList<>();
ArrayList<Integer> path = new ArrayList<>();
if (root == null) return res;
traversal(root, path, res);
return res;
}
public static void traversal(TreeNode cur, ArrayList<Integer> path, ArrayList<String> res) {
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));
res.add(sb.toString());
return;
}
if (cur.left != null) {
traversal(cur.left, path, res);
path.remove(path.size() - 1);
}
if (cur.right != null){
traversal(cur.right, path, res);
path.remove(path.size() - 1);
}
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/neo1001/leet-code-practice-record.git
git@gitee.com:neo1001/leet-code-practice-record.git
neo1001
leet-code-practice-record
LeetCode刷题记录
master

Search