leetcode257. Binary Tree Paths
来源:互联网 发布:ubuntu des 解密 编辑:程序博客网 时间:2024/05/18 12:34
257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree: 1 / \2 3 \ 5All root-to-leaf paths are:["1->2->5", "1->3"]
解法
dfs,直到叶子结点,生成字符串
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> answer = new ArrayList<>(); if (root == null) { return answer; } searchBT(root, "", answer); return answer; } public void searchBT(TreeNode root, String path, List<String> answer) { if (root.left == null && root.right == null) { answer.add(path + root.val); } if (root.left != null) { searchBT(root.left, path + root.val + "->", answer); } if (root.right != null) { searchBT(root.right, path + root.val + "->", answer); } }}
阅读全文
0 0
- [LeetCode257] Binary Tree Paths
- leetcode257 Binary Tree Paths
- leetcode257---Binary Tree Paths
- LeetCode257:Binary Tree Paths
- LeetCode257. Binary Tree Paths
- leetcode257 Binary Tree Paths
- leetcode257. Binary Tree Paths
- LeetCode257——Binary Tree Paths
- LeetCode257. Binary Tree Paths(dfs)
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- Binary Tree Paths
- [leetcode] Binary Tree Paths
- 257 Binary Tree Paths
- Binary Tree Paths
- SURF特征提取算法原文翻译与理解(上)
- 工业物联网的实施有哪些关键要素?
- 进程池&线程池原理及应用
- Spring框架中的单例Beans是线程安全的么
- [OpenGL 超级宝典][笔记] 8.2 帧缓冲区(FBO)
- leetcode257. Binary Tree Paths
- Android热修复原理
- 中国剩余定理
- bzoj1726 [Usaco2006 Nov]Roadblocks第二短路
- 学习
- kotlin汇总10-Java调用kotlin
- 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作
- Codeforces 811 A Vladik and Courtesy
- IPC之消息队列