[leetcode] 257. Binary Tree Paths
来源:互联网 发布:lol代练接单软件 编辑:程序博客网 时间:2024/06/05 12:50
题目:
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"]1.采用递归左子树递归找到路径(根节点+左子树)右子树递归找到路径(根节点+右子树)2.递归停止条件:递归到叶子节点时终止,即root.left==null && root.right == null时,将字符串add到result中。
public List<String> binaryTreePaths(TreeNode root){List<String> list=new ArrayList<String>();findPath(root,"",list);return list;}public void findPath(TreeNode node,String curPath,List<String> list){if(node==null)return;if(node.left==null&&node.right==null){list.add(curPath+node.val);return;}findPath(node.left,curPath+node.val+"->",list);findPath(node.right,curPath+node.val+"->",list);}
阅读全文
0 0
- [leetcode] 257. Binary Tree Paths
- 257. Binary Tree Paths LeetCode
- 【LeetCode】257. Binary Tree Paths
- LeetCode 257. Binary Tree Paths
- LeetCode *** 257. Binary Tree Paths
- leetcode 257. Binary Tree Paths
- LeetCode 257. Binary Tree Paths
- Leetcode 257. Binary Tree Paths
- LeetCode-257.Binary Tree Paths
- 【leetcode】257. Binary Tree Paths
- [Leetcode]257. Binary Tree Paths
- LeetCode - 257. Binary Tree Paths
- LeetCode 257. Binary Tree Paths
- [leetcode] 257. Binary Tree Paths
- LeetCode 257. Binary Tree Paths
- 257.[Leetcode]Binary Tree Paths
- LeetCode 257. Binary Tree Paths
- leetcode 257. Binary Tree Paths
- Dasha and Stairs
- C#中数组的属性和方法
- 框架学习之struts2-05文件上传、下载、ajax整合json
- Golang爬虫基础:通过代理IP发送请求
- CSS测试题
- [leetcode] 257. Binary Tree Paths
- DOM编程-事件(三)
- Linux 查找工具 find、grep
- 【简记】Java Web 内幕——Mybatis框架小结
- H5 学习笔记2
- DOM编程-事件(四)
- 【3dsmax】物体冻结与半透明显示
- sql2000备份数据库文件如何还原到sql2008数据库中
- git(七)-远程与github