您的位置:首页 > 其它

[LeetCode]257. Binary Tree Paths

2017-02-03 19:10 260 查看
https://leetcode.com/problems/binary-tree-paths/

找出二叉树根节点到叶子节点的所有路径

public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> res = new LinkedList();
if (root != null) {
search(root, res, "");
}
return res;
}
private void search(TreeNode root, List<String> res, String part) {
if (root.left == null && root.right == null) {
res.add(part + root.val);
}
if (root.left != null) {
search(root.left, res, part + root.val + "->");
}
if (root.right != null) {
search(root.right, res, part + root.val + "->");
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: