Algorithms—257.Binary Tree Paths
来源:互联网 发布:cad网络图标 编辑:程序博客网 时间:2024/05/21 08:41
思路:找到所有的路径,然后读数。
/** * 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> ansList=new ArrayList<String>(); if (root!=null) {List<List<TreeNode>> totalList=new ArrayList<List<TreeNode>>();List<TreeNode> initList=new ArrayList<TreeNode>();initList.add(root);totalList.add(initList);for (int i = 0; i < totalList.size(); i++) {List<TreeNode> list=totalList.get(i);TreeNode node=list.get(list.size()-1);boolean flag=false;if (node.left!=null) {List<TreeNode> leftList=new ArrayList<TreeNode>();leftList.addAll(list);leftList.add(node.left);totalList.add(leftList);flag=true;}if (node.right!=null) {List<TreeNode> rightList=new ArrayList<TreeNode>();rightList.addAll(list);rightList.add(node.right);totalList.add(rightList);flag=true;}if (flag) {totalList.remove(i);i--;}}for (int i = 0; i < totalList.size(); i++) {List<TreeNode> list=totalList.get(i);String ans=""+list.get(0).val;for (int j = 1; j < list.size(); j++) {ans+=("->"+list.get(j).val);}ansList.add(ans);}} return ansList; }}
耗时:312ms。
0 0
- Algorithms—257.Binary Tree Paths
- [LeetCode] Algorithms-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
- js内置对象属性及方法
- HDU2426 Interesting Housing Problem(KM匹配 )
- 惠普第一周
- iOS代码常用的代码片段
- Lucene-Java全文搜索引擎
- Algorithms—257.Binary Tree Paths
- 利用栈可以进行平衡符号的检测
- lua字符串操作
- 有哪些不错的C语言面试题?
- WebStorm使用less
- 【产品进阶】自己如何获取数据+分析数据
- HDU 5347 MZL's chemistry(水~)
- solr入门教程
- Spring依赖注入