Binary Tree Paths ---LeetCode
来源:互联网 发布:bugs软件 编辑:程序博客网 时间:2024/06/06 16:54
https://leetcode.com/problems/binary-tree-paths/
解题思路:
这道题需要求返回所有从根节点到叶子节点的路径,用递归实现。求路径的题目思想都大差不差,注意细节就好。
/** * 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 ArrayList<>(); public List<String> binaryTreePaths(TreeNode root) { if (root == null) return result; helper(root, String.valueOf(root.val)); return result; } public void helper(TreeNode root, String path) { if (root.left == null && root.right == null) result.add(path); if (root.left != null) helper(root.left, path + "->" + root.left.val); if (root.right != null) helper(root.right, path + "->" + root.right.val); }}
0 0
- Leetcode: Binary Tree Paths
- [LeetCode] Binary Tree Paths
- [Leetcode]Binary Tree Paths
- leetcode:Binary Tree Paths
- [leetcode] Binary Tree Paths
- Leetcode: Binary Tree Paths
- LeetCode:Binary Tree Paths
- leetcode Binary Tree Paths
- leetcode Binary Tree Paths
- LeetCode:Binary Tree Paths
- LeetCode Binary Tree Paths
- LeetCode Binary Tree Paths
- LeetCode Binary Tree Paths
- LeetCode || Binary Tree Paths
- *LeetCode-Binary Tree Paths
- Binary Tree Paths -- leetcode
- LeetCode---Binary Tree Paths
- leetcode-Binary Tree Paths
- 由HITCON 2016一道web聊一聊php反序列化漏洞
- Android Studio常用快捷键(提取全局变量等)
- Windows(gitlab为例)系统下git常用操作
- yii2 实现简单的留言板组件
- java--过滤器
- Binary Tree Paths ---LeetCode
- 区块链开源项目Ripple四、共识(1)
- python第一课笔记
- log4j.properties 详解与配置步骤
- yii2 实现图片轮播组件
- Java之设计模式六大原则(2):里氏替换原则
- TCP连接的建立(三次握手)和释放(四次握手)
- GridView中实现CheckBoxList选中更新数据库
- 前端优化