Binary Tree Paths
来源:互联网 发布:csol由于网络设定问题 编辑:程序博客网 时间:2024/05/29 18:52
import java.util.LinkedList;import java.util.List;import java.util.Queue;/* * 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"] * */public class Solution {public static void main(String[] args) {// TODO Auto-generated method stub}public List<String> binaryTreePaths(TreeNode root) { List<String> list = new LinkedList<>(); if(root == null) return list; getPaths("", root, list); return list; }public void getPaths(String path,TreeNode root,List<String> list){if(root.left==null&&root.right==null)//is a leaf{path = path + root.val;list.add(path);}path = path +root.val+"->";//normal nodeif(root.left!=null)//leftgetPaths(path, root.left, list);if(root.right!=null)//rightgetPaths(path, root.right, list);}}class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}
0 0
- 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
- [LeetCode257] Binary Tree Paths
- Binary Tree Paths
- Binary Tree Paths
- Leetcode: Binary Tree Paths
- LeetCode:Binary Tree Paths
- leetcode Binary Tree Paths
- leetcode Binary Tree Paths
- LeetCode:Binary Tree Paths
- Binary Tree Paths
- 异步加载图片
- 加强版Apktool堪称逆向神器
- 我的第一篇博客
- IMX6Q LTIB 安装编译错误的解决办法
- Gson搭配TypeToken实现复杂数据的解析
- Binary Tree Paths
- ubuntu下搭建android开发环境(四)核心篇安装AndroidStudio、sdk、jdk(by 星空武哥)
- 对虚函数的初步理解
- 静态库,共享库,动态加载库
- TestNG的IInvokedMethodListener监听器详解
- Nginx+Tomcat+Redis负载均衡集群服务搭建
- 数据结构与算法(9)顺序查找
- UVA 10817 Headmaster's Headache
- Ubuntu上安装oracle java 7