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"]

给一个二叉树,返回所有根到叶节点的路径。

Java:

/**
* 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 + "->");
}
} 

Python:

class Solution:
# @param {TreeNode} root
# @return {string[]}
def binaryTreePaths(self, root):
result, path = [], []
self.binaryTreePathsRecu(root, path, result)
return result def binaryTreePathsRecu(self, node, path, result):
if node is None:
return if node.left is node.right is None:
ans = ""
for n in path:
ans += str(n.val) + "->"
result.append(ans + str(node.val)) if node.left:
path.append(node)
self.binaryTreePathsRecu(node.left, path, result)
path.pop() if node.right:
path.append(node)
self.binaryTreePathsRecu(node.right, path, result)
path.pop()

C++: DFS

class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if (root) dfs(root, "", res);
return res;
}
void dfs(TreeNode *root, string out, vector<string> &res) {
out += to_string(root->val);
if (!root->left && !root->right) res.push_back(out);
else {
if (root->left) dfs(root->left, out + "->", res);
if (root->right) dfs(root->right, out + "->", res);
}
}
};

C++:

class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
if (!root) return {};
if (!root->left && !root->right) return {to_string(root->val)};
vector<string> left = binaryTreePaths(root->left);
vector<string> right = binaryTreePaths(root->right);
left.insert(left.end(), right.begin(), right.end());
for (auto &a : left) {
a = to_string(root->val) + "->" + a;
}
return left;
}
};

 

类似题目:

[LeetCode] 112. Path Sum 路径和

[LeetCode] 113. Path Sum II 路径和 II

[LeetCode] 437. Path Sum III 路径和 III

 

All LeetCode Questions List 题目汇总

[LeetCode] 257. Binary Tree Paths 二叉树路径的更多相关文章

  1. [leetcode]257. Binary Tree Paths二叉树路径

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  2. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

  3. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  4. LeetCode 257. Binary Tree Paths (二叉树路径)

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  5. LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  6. [LeetCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  7. 257 Binary Tree Paths 二叉树的所有路径

    给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树:   1 /   \2     3 \  5所有根到叶路径是:["1->2->5", " ...

  8. 【easy】257. Binary Tree Paths 二叉树找到所有路径

    http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...

  9. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

随机推荐

  1. Nginx——报错汇总

    前言 记录NGINX的错误 错误 nginx: [emerg] unknown directive "erver" in /usr/local/nginx/conf/vhost/d ...

  2. TAPD---“文档”的用途

    主要用途:文件的存放 (1)对于测试组:存放测试用例.主要针对当前的迭代,可新建对应的文件夹,上传存放相应的xmind.excel文件.方便开发查找用例文件 (2)对于项目:存放共用的文档等 这里只是 ...

  3. 36氪新风向 | 三个月估值普涨三倍,你未来的RPA机器人同事正在路上

    http://www.sohu.com/a/320208242_114778 2019 年 4 月,关注 RPA 赛道的投资人 Kevin(化名)就发现,仅仅过了一个春节,自己所在的早期基金已经投不起 ...

  4. Vue --- 指令练习

    scores = [ { name: 'Bob', math: 97, chinese: 89, english: 67 }, { name: 'Tom', math: 67, chinese: 52 ...

  5. Tips on Python

    python是一种解释性文件,代码要通过解释器解释运行.python解释器就是python.exe这个程序. pip也是一个pip.exe的程序,是用来管理python的第三方库. 有两种执行方式:脚 ...

  6. oracle 按每天,每周,每月,每季度,每年查询统计数据

    oracle 按每天,每周,每月,每季度,每年查询统计数据 //按天统计 select count(dataid) as 每天操作数量, sum() from tablename group by t ...

  7. 关于System.Reflection.TargetInvocationException 异常

    什么是TargetInvocationException 由通过反射调用的方法引发的异常. 继承 Object Exception ApplicationException TargetInvocat ...

  8. 开源项目 10 CSV

    using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Data; using Syst ...

  9. 处理 MySQL 因为 SLAVE 崩溃导致需要手动跳过 GTID 的问题 | 关于 GTID

    今天发生了与之前某篇博客相似的问题,有同学在不同步的 binlog 库中使用语句 database.table 命令对表进行 drop 导致 master 丢弃该表但是从库并未能同步到该操作.并且后续 ...

  10. 求斐波那契数列中的第N个数

    递推 递归 1.暴力递归 2.记忆化递归 对比下二者的效率