[LeetCode257] Binary Tree Paths
来源:互联网 发布:伟哥影响生育吗 知乎 编辑:程序博客网 时间:2024/05/18 03:59
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"]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { List<String> result = new LinkedList<>(); List<Integer> path = new LinkedList<>(); public List<String> binaryTreePaths(TreeNode root) { if(root ==null) return result; getPaths(root); return result; } public void getPaths(TreeNode node){ if(node == null) return; path.add(node.val); if(node.left == null && node.right == null){ StringBuffer buffer = new StringBuffer(); for(int i=0;i<path.size();i++){ if(i!=0) buffer.append("->"); buffer.append(path.get(i)); } result.add(buffer.toString()); } getPaths(node.left); getPaths(node.right); path.remove(path.size()-1); }}
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
- 81 css ie8无法识别背景图片
- Jfinal使用Freemarker配置处理NULL、重起生效等问题
- 2015年至关重要的网页版式设计趋势
- HDU 5411 CRB and Puzzle (2015年多校比赛第10场)
- HDOJ 1869 六度分离(最短路之floyd)
- [LeetCode257] Binary Tree Paths
- 【c++版数据结构】之单链表的实现(带头结点以及尾节点)
- WEB测试总结
- UVA 540 Team Queue
- 一点一点学习struts1(一)_简介
- 黑马程序员-----Java基础-----Map
- hdu5414(2015多校10)--CRB and String(字符串匹配)
- leetcode——Two Sum_1
- Cocos2d-x 3.7 Android&iOS开发环境搭建