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"]
代码如下:
/**
* 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:
//按照容易自己习惯的数据结构来存储路径,最后做一个转换
void findPath(TreeNode* t,vector<int>& temp, vector<vector<int> >& record)
{
temp.push_back(t->val);
if(t->left!=NULL) findPath(t->left, temp, record);
if(t->right!=NULL) findPath(t->right, temp, record);
//到达叶子节点,就记录路径
if(t->left==NULL && t->right==NULL)
{
record.push_back(temp);
temp.erase(temp.end()-1);
return ;
}
//中间节点返回
temp.erase(temp.end()-1);
return;
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<vector<int>> record;
vector<string> srecord;
if(root==NULL) return srecord;
vector<int> temp;
//找到所有的路
findPath(root,temp,record);
//把所有的路按照题目要求存储
string stemp;
for(int i=0;i<record.size();i++)
{
stringstream ss;
for(int j=0;j<record[i].size();j++)
{
ss<<record[i][j]<<"->";
}
stemp.clear();
stemp=ss.str();
//去掉最后的"->"
stemp.pop_back();
stemp.pop_back();
srecord.push_back(stemp);
}
return srecord;
}
};
Leetcode题 257. Binary Tree Paths的更多相关文章
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- LeetCode OJ 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->5","1->3"] 思路 递归地返回左右子树到叶子节 ...
- <LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- 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. For example, given the following binary tree: 1 ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- Vue.use()源码分析且执行后干什么了
直接开始分析源码 // Vue源码文件路径:src/core/global-api/use.js import { toArray } from '../util/index' //initUse函数 ...
- BPM业务流程管理系统_K2受邀出席QAD客户日活动,赋能企业云端智造_工作流引擎
10月17日,K2受邀参加由厦门易维主办的以“走进QAD云ERP,深耕智能制造”为主题的QAD客户日活动.本次大会是以工业4.0背景下传统制造业面临巨大压力和挑战为导向,旨在探讨如何助力企业迅速适应业 ...
- ArduPilot简介
源码地址:https://github.com/ArduPilot/ardupilot/ 参考:http://ardupilot.org/dev/docs/learning-the-ardupilot ...
- Jerry带您了解Restful ABAP Programming模型系列之三:云端ABAP应用调试
Jerry的Restful ABAP Programming模型介绍系列的前两篇文章: 30分钟用Restful ABAP Programming模型开发一个支持增删改查的Fiori应用 Jerry带 ...
- FPGA学习笔记之按键控制
参考: [黑金原创教程][FPGA那些事儿-驱动篇I ]实验二:按键模块① - 消抖 源码如下: key_funcmod.v module key_funcmod(clk, rst, key, led ...
- Yum三方仓库——EPEL
参考:什么是EPEL 及 Centos上安装EPEL 参考:How to Enable EPEL Repository for RHEL/CentOS 7.x/6.x/5.x 前言 RHEL以及他的衍 ...
- 【OF框架】缓存Session/Cookies/Cache代码调用api,切换缓存到Redis
准备 缓存服务在应用开发中最常用的功能,特别是Session和Cookies,Cache部分业务开发过程会使用到. 在负载均衡环境下,缓存服务需要存储到服务器. 缓存默认实现在内存在,可以通过配置切换 ...
- Linux文件系统概览
三 Linux文件系统概览 3.1 Linux文件系统简介 在Linux操作系统中,所有被操作系统管理的资源,例如网络接口卡.磁盘驱动器.打印机.输入输出设备.普通文件或是目录都被看作是一个文件. 也 ...
- Python下载图片并保存本地的两种方式
一:使用Python中的urllib类中的urlretrieve()函数,直接从网上下载资源到本地,具体代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- python_面向对象——双下划线方法
1.__str__和__repe__ class Person(object): def __init__(self,name,age): self.name = name self.age = ag ...