[刷题] 257 Binary Tree Paths
要求
- 给定一棵二叉树,返回所有表示从根节点到叶子节点路径的字符串
示例
- ["1->2->5","1->3"]

思路
- 递归地返回左右子树到叶子节点的字符串
示例
1 class Solution {
2 public:
3 vector<string> binaryTreePaths(TreeNode* root) {
4
5 vector<string> res;
6
7 if( root == NULL )
8 return res;
9
10 if( root->left == NULL && root->right == NULL){
11 res.push_back( to_string(root->val) );
12 return res;
13 }
14
15 vector<string> leftS = binaryTreePaths(root->left);
16 for( int i = 0 ; i < leftS.size() ; i ++ )
17 res.push_back( to_string(root->val) + "->" + leftS[i]);
18
19 vector<string> rightS = binaryTreePaths(root->right);
20 for( int i = 0 ; i < rightS.size() ; i ++ )
21 res.push_back( to_string(root->val) + "->" + rightS[i]);
22
23 return res;
24 }
25 };
相关
- 113 Path Sum II
- 129 Sum Root to Leaf Numbers
[刷题] 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 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
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- 257. Binary Tree Paths返回所有深度优先的遍历
[抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- 257. Binary Tree Paths
题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...
- 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 ...
随机推荐
- SQL语句练习(进阶版)
学生数据库中有三个基本表(关系)如下: 学生表S(Sno,Sname,Age,Sex,SD) 课程表C(Cno,Cname, Teacher) 选课表SC(Sno,Cno,Grade) 请用SQL语言 ...
- 可视化运行Python的神器Jupyter Notebook
目录 简介 Jupyter Notebook 启动notebook server notebook document 的结构 code cells markdown cells raw cells 以 ...
- PAT B1039/A1092 到底买不买项链
小红买些珠子做项链,但是卖家不肯拆散了卖,于是帮忙判断一下,某串珠子是否全部包含自己想要的珠子,如果是告诉她有多少多余的珠子,如果不是,又缺了那些珠子现在为了方便起见用"0-9"& ...
- 在Visual Studio 中使用git——什么是Git(一)
写程序必然需要版本控制,哪怕是个人项目也是必须的,微软从Visual Studio 2019开始默认提供了对Git的支持,Visual Studio 2019之前的版本可以安装相应的插件来实现Git功 ...
- 自动化kolla-ansible部署centos7.9+openstack-train-超融合高可用架构
自动化kolla-ansible部署centos7.9+openstack-train-超融合高可用架构 欢迎加QQ群:1026880196 进行交流学习 环境说明: 1. 满足一台电脑一个网卡的环境 ...
- SOAP与REST API的区别
之前一直想写些RESTful相关的文章,却一直未付诸行动.直到最近接手一个新的项目,对这块重新温习,乘此机会写些相关帖子.欢迎大家评论留言,如有错漏之处,也望各位大佬不令赐教. 话不多说,干活顶上. ...
- Python的文本和字节序列
一.字符串的表示和存储 字符串是字符的序列,每个字符都有有一个数字作为标识,同时会有一个将标识转换为存储字节的编码方案: s = 'hello world python' for c in s: pr ...
- 吉特日化MES&WMS系统--三色灯控制协议转http
关于硬件控制大部分都是使用CS客户端程序,一般连接口都是用网口,串口,USB口等,应用通讯是不支持HTTp协议操作的,而目前一般做技术的人员都在于BS开发,使用HTTP 协议,所以在硬件交互上可能觉得 ...
- Rabbitmq 3.6.5以上版本修改端口号方法
Rabbitmq 3.6.5以上版本修改端口号方法,网上查了下有一些方不管用,所以自己实践了引用官网说明 https://www.rabbitmq.com/configure.html#configu ...
- 996. Number of Squareful Arrays
Given an array A of non-negative integers, the array is squareful if for every pair of adjacent elem ...