Leetcode 257 -- Binary Tree Paths
来源:互联网 发布:激光slam算法 编辑:程序博客网 时间:2024/06/05 21: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"]
解法: 继续用深度搜索的方法。注意这里的递归过程,各自数据结构之间的转换。
/** * 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> res = new ArrayList<String>(); if(root == null){ return res; } String cur = new String(); cur += Integer.toString(root.val); helper(root, res, cur); return res; } public void helper(TreeNode root, List<String> res, String cur){ if(root == null) return; if(root.left == null && root.right == null){ res.add(new String(cur)); return; } if(root.left != null){ helper(root.left, res, cur + "->" + Integer.toString(root.left.val)); } if(root.right != null){ helper(root.right, res, cur + "->" + Integer.toString(root.right.val)); } }}
0 0
- 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
- leetcode-257-Binary Tree Paths
- LeetCode 257Binary Tree Paths
- LeetCode 257 Binary Tree Paths
- leetcode 257 Binary Tree Paths
- LeetCode-257:Binary Tree Paths
- 关于GCD的学习笔记
- HDU 5873 Football Games(签到题)——2016 ACM/ICPC Asia Regional Dalian Online
- Tomcat 类加载器结构
- C语言 顺序表的实现(静态)
- 7.15 N,O,P,Q Codeforces 691 A,B,C,D
- Leetcode 257 -- Binary Tree Paths
- 设计模式(GOF)之我见(1)——Factory
- 【Qt开发】Qt应用程序发布封装
- hdu5875 Function (预处理+单调性质)
- iOS开发 - Quartz2D绘图
- 【算法学习】纯高斯模糊算法处理灰度图片
- 【BLE-CC2640】CC2640之微信硬件快速入门
- 快速排序(quicksort)
- HDU1271-Arbitrage