[LeetCode]257. Binary Tree Paths
来源:互联网 发布:苏州收银软件sjzpos 编辑:程序博客网 时间:2024/06/05 04:47
题目描述: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”]
解题思路:深度优先遍历,如果当前节点为叶子结点,则将结果添加到list中。否则继续递归遍历。
public List<String> binaryTreePaths(TreeNode root) { List<String> answer = new ArrayList<String>(); if(root!=null)searchBT(root,"",answer); return answer; } public void searchBT(TreeNode root,String path,List<String> answer){ if(root.left==null&&root.right==null)answer.add(path+root.val); if(root.left!=null)searchBT(root.left,path+root.val+"->",answer); if(root.right!=null)searchBT(root.right,path+root.val+"->",answer); } class TreeNode{ TreeNode left; TreeNode right; int val; public TreeNode(int x){ val = x; } }
阅读全文
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
- Mybatis绑定错误的原因
- android gif动态图显示
- 欢迎使用CSDN-markdown编辑器
- Java中double类型比较大小或相等的方法
- xml和json互转的工具类
- [LeetCode]257. Binary Tree Paths
- Dialog确认按钮不dismiss
- 验证邮箱是否存在工具类
- Python使用argparse命令行选项计算文件中单词个数
- Jsp与Servlet的区别
- 调用dubbo服务提示:java.io.IOException: invalid constant type: 18
- 有道云笔记修改背景图片
- Sqlite事物与锁
- vim 删除一整块,vim 删除一整行