[LeetCode] 257. Binary Tree Paths 二叉树路径
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] 113. Path Sum II 路径和 II
[LeetCode] 437. Path Sum III 路径和 III
All LeetCode Questions List 题目汇总
[LeetCode] 257. Binary Tree Paths 二叉树路径的更多相关文章
- [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 ...
- Leetcode 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 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 ...
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 257 Binary Tree Paths 二叉树的所有路径
给定一个二叉树,返回从根节点到叶节点的所有路径.例如,给定以下二叉树: 1 / \2 3 \ 5所有根到叶路径是:["1->2->5", " ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- Vuex 是什么?
Vuex 是什么? Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式.它采用集中式存储管理应用的所有组件状态,并以相应的规则保证状态以一种可预测的方式发生变 什么是"状态管 ...
- Visual Studio Code 写Python代码
之前用nodepad++,sublime text3,ultraedit,最近上手微软的vsc感觉上手还行,如果没有pycharm照样可以使用它 https://code.visualstudio.c ...
- Windows环境下设置Tomcat8以服务的形式运行,不再打开Tomcat窗口
内容简介 在Windows操作系统下,设置Tomcat8以服务的形式运行,按照以下3步来操作即可.前提条件:已安装好Java环境,并配置好java的环境变量:已下载好Tomcat8并解压到某目录. s ...
- Comet OJ - Contest #14 转转的数据结构题 珂朵莉树+树状数组
题目链接: 题意:有两个操作 操作1:给出n个操作,将区间为l到r的数字改为x 操作2:给出q个操作,输出进行了操作1中的第x到x+y-1操作后的结果 解法: 把询问离线,按照r从小到大排序 每次询问 ...
- L3956棋盘
1,记得之前要复习.上次先写的题是数的划分. 虽然我不想说,估计全忘了.复习就当把上次的题写了把. 应该比较稳了. 2,题中的要求. 一,所在的位置必须是有颜色的.(很明显要用bool去涂一遍) 二, ...
- 洛谷 SP740 TRT - Treats for the Cows 题解
SP740 TRT - Treats for the Cows 题目描述 FJ has purchased N (1 <= N <= 2000) yummy treats for the ...
- 【loj2983】【WC2019】数树
题目 两颗\(n\)个点的树T1和T2,有\(y\)种颜色; 现在给每个点染色,要求公共边端点的颜色相同,求: 1.op=0 , T1和T2都确定,求合法染色方案数: 2.op=1 , T1确 ...
- jmeter5实现mysql数据库值提取--单sql提取
字段背景: 在进行接口测试或者压力测试过程中下文的请求需要用到上文请求的值,除了通过正则表达式的方式外,为了更准确的获得数据库值,我们可以直接从数据库提取 一.如何实现数据库的连接.此处不再赘述 点击 ...
- c++中关于堆和堆栈的区别
在C++中,内存分成5个区,他们分别是堆.栈.自由存储区.全局/静态存储区和常量存储区. 栈,就是那些由编译器在需要的时候分配,在不需要的时候自动清楚的变量 的存储区.里面的变量通常是局部 ...
- if, if/else, if /elif/else,case
一.if语句用法 if expression then command fi 例子:使用整数比较运算符 read -p "please input a integer:" a if ...