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. Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)

    题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...

  2. 使用flask搭建微信公众号:实现签到功能

    终于到了实战阶段.用微信公众号实现一个简单的签到功能. 前情提要: 微信公众号token验证失败 使用flask搭建微信公众号:完成token的验证 使用flask搭建微信公众号:接收与回复消息 程序 ...

  3. Python 进程共享数据(数据传输)实例

    #coding:utf-8 ''' Created on 2017年11月22日 @author: li.liu ''' import multiprocessing from time import ...

  4. Alpha冲刺(10/10)——追光的人

    1.队友信息 队员学号 队员博客 221600219 小墨 https://www.cnblogs.com/hengyumo/ 221600240 真·大能猫 https://www.cnblogs. ...

  5. CentOS6.5配置

    关闭防火墙 查看防火墙状态 /etc/init.d/iptables status 停止 /etc/init.d/iptables stop 开机不启动 chkconfig iptables off ...

  6. LightOJ - 1322 - Worst Case Trie(DP)

    链接: https://vjudge.net/problem/LightOJ-1322 题意: In Computer Science Trie or prefix tree is a data st ...

  7. python 数据分析

    pandas 格式化数据的读取 numpy 提供数组处理,类似matlap matplotlib 数据可视化 https://www.cnblogs.com/5poi/p/7148000.html

  8. jsp大附件上传,支持断点续传

    我们平时经常做的是上传文件,上传文件夹与上传文件类似,但也有一些不同之处,这次做了上传文件夹就记录下以备后用. 这次项目的需求: 支持大文件的上传和续传,要求续传支持所有浏览器,包括ie6,ie7,i ...

  9. 洛谷 P5506 封锁

    目录 题目 思路 \(Code\) 题目 P5506 封锁 思路 模拟 \(\large\text{读题一定要细心}\) 解释都在代码里. \(Code\) #include<bits/stdc ...

  10. 一天一经典Efficient Estimation of Word Representations in Vector Space

    摘要 本文提出了两种从大规模数据集中计算连续向量表示(Continuous Vector Representation)的计算模型架构.这些表示的有效性是通过词相似度任务(Word Similarit ...