257. Binary Tree Paths
来源:互联网 发布:java未来五年发展前景 编辑:程序博客网 时间:2024/05/22 17:17
Given a binary tree, return all root-to-leaf paths.
For example, given the following binary tree:
1 / \2 3 \ 5
All root-to-leaf paths are:
["1->2->5", "1->3"]
思路:就是一个回溯的dfs、
代码如下(已通过leetcode)
public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> list=new ArrayList<String>();
if(root==null) return list;
String s="";
DFS(root,s,list);
return list;
}
public void DFS(TreeNode root,String s,List<String> list) {
if(root.left==null && root.right==null) {
s=s+root.val;
list.add(s);
s="";
} else {
s=s+root.val+"->";
if(root.left!=null) {
DFS(root.left,s,list);
}
if(root.right!=null) {
DFS(root.right,s,list);
}
}
}
}
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 257. Binary Tree Paths
- 11g: SQL Fundamentals I 1Z0-051
- eclipse配置NDK环境之语法提示和自动编译
- 04-单进程SAPI生命周期
- ASP.NET初始
- 练习2 24题
- 257. Binary Tree Paths
- hdoj 4276 The Ghost Blows Light 【树形dp】
- 1018 Tempter of the Bone
- 深入浅出--C\C++指针的本质
- itextsharp整理
- 关于“性能计数器注册表单元配置一致性”检测失败解决办法
- hexo和github打造个人博客
- js中判断数据类型的属性typeof、instanceof、 constructor、 prototype
- PSobj [9] 这几天补了一下基础知识 放一些练习