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:
Input: 1
/ \
2 3
\
5 Output: ["1->2->5", "1->3"] Explanation: All root-to-leaf paths are: 1->2->5, 1->3
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> res;
if (!root) return res; binaryTreePaths(res, root, to_string(root->val)); return res;
} void binaryTreePaths(vector<string>& res, TreeNode* root, string t)
{
if (!root->left && !root->right)
{
res.push_back(t);
return;
} if (root->left) binaryTreePaths(res, root->left, t + "->" + to_string(root->left->val));
if (root->right) binaryTreePaths(res, root->right, t + "->" + to_string(root->right->val));
} };
LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)的更多相关文章
- [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 257 Binary Tree Paths 二叉树 DFS
找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...
- LeetCode 257. 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", " ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- (easy)LeetCode 257.Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- Java [Leetcode 257]Binary Tree Paths
题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
随机推荐
- Vmware克隆Centos6.5虚拟机网卡无法启动问题
1.编辑eth0的配置文件:vi /etc/sysconfig/network-scripts/ifcfg-eth0,删除HWADDR地址那一行及UUID的行如下: #HWADDR=:0c::::9f ...
- Cobbler--自动化部署
Cobbler自动化部署 cobbler简介 Cobbler 可以用来快速建立 Linux 网络安装环境,它已将Linux网络安装的技术门槛,从大专以上文化水平,成功降低到了初中水平,连补鞋匠都能学会 ...
- wentaolovesmeng.club
wentaolovesmeng.club wentaostudy.club
- chrome开发者工具--使用 Network 面板测量您的网站网络性能。
转自:Tools for Web Developers Network 面板记录页面上每个网络操作的相关信息,包括详细的耗时数据.HTTP 请求与响应标头和 Cookie,等等. TL;DR 使用 ...
- 洛谷 P3071 [USACO13JAN]座位Seating(线段树)
P3071 [USACO13JAN]座位Seating 题目链接 思路: 一开始把题给读错了浪费了好多时间呜呜呜. 因为第二个撤离操作是区间修改,所以我们可以想到用线段树来做.对于第一个操作,我们只需 ...
- iwlist/iwconfig/iw命令
1.iwlist 命令:用于对/proc/net/wireless文件进行分析,得出无线网卡相关信息 # iwlist wlan0 scanning 搜索当前无线网络 # iwlist wlan0 f ...
- Spring Boot 之:接口参数校验
Spring Boot 之:接口参数校验,学习资料 网址 SpringBoot(八) JSR-303 数据验证(写的比较好) https://qq343509740.gitee.io/2018/07/ ...
- 调皮的js
一.中文变量名 今天偶然看文章.看到js的var还可以用中文做变量名,那么我们试试,看看中文的变量名是否能打印出值呢? var 临时变量="111"; console.log(临时 ...
- 编程语言和python介绍, 变量,小整数池,垃圾回收机制
1.编程语言的发展史 计算机是基于电工作(基于高.低电平)1010010101011 1.机器语言 优点:执行速度够快 缺点:开发效率非常低 2.汇编语言(通过英文字符组成) 优点:执行效率相较于机器 ...
- day5 SpringSecurity权限控制jsr250注解不起作用 AOP日志排除不需要织入的方法 web.xml配置错误码页面