257. Binary Tree Paths | 二叉树的路径
来源:互联网 发布:python离线安装包下载 编辑:程序博客网 时间:2024/05/22 01:41
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"]
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
Subscribe to see which companies asked this question.
思路:采用深度优先遍历。
/** * 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> list = new ArrayList<>();binaryTreePathsHelper(root, list, new String());return list;}public void binaryTreePathsHelper(TreeNode root, List<String> list, String string) {if (root == null) {return;}if (root.left == null && root.right == null) {string = string+ root.val;list.add(string);return;}binaryTreePathsHelper(root.left, list, string + root.val + "->");binaryTreePathsHelper(root.right, list, string + root.val + "->");}}
0 0
- 257. Binary Tree Paths 二叉树的路径
- 257. Binary Tree Paths | 二叉树的路径
- Binary Tree Paths (二叉树路径遍历)
- lintcode binary-tree-paths 二叉树的所有路径
- Binary Tree Paths(二叉树的路径)
- 【Leetcode】257 Binary Tree Paths 二叉树的路径
- 257. Binary Tree Paths(打印二叉树所有路径)
- 257. Binary Tree Paths打印二叉树路径
- Tree----- 257. Binary Tree Paths(二叉树从根节点开始的所有路径)
- 257. Binary Tree Paths (求二叉树中所有根到叶子的路径)
- 【二叉树】257. Binary Tree Paths
- LeetCode OJ 之 Binary Tree Paths(二叉树路径)
- Binary Tree Paths 二叉树的路径(含创建树的代码)
- Binary Tree Paths 二叉树所有从跟节点到叶子节点的路径
- 480.Binary Tree Paths-二叉树的所有路径(容易题)
- 257.leetcode Binary Tree Paths(easy)[二叉树深度路径遍历]
- 【二叉树】字符串打印树结构/路径【606. Construct String from Binary Tree】【257. Binary Tree Paths】【102. Binary Tree Le
- Binary Tree Paths 二叉树遍历
- Java 集成Ftp Server踩到的坑
- ACM小学生算术
- 关于Android:layout_weight的真实含义
- 剑指offer题二
- 中文数字转换成阿拉伯数字。
- 257. Binary Tree Paths | 二叉树的路径
- Velocity代码片段
- 札记2
- 浅谈Android源码与设计模式
- @JSONField
- ubuntu16.04空间扩容
- 剑指offer题七
- 设置ODBC数据源时候更改默认数据库时没有新建的数据库
- Leetcode :palindrome partitioning(c++)