[刷题] 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 ...
随机推荐
- [BFS]A. 【例题1】走迷宫
A . [ 例 题 1 ] 走 迷 宫 解析 简单的BFS模板题 Code #include <bits/stdc++.h> #define N 1005 using namespace ...
- Distributed | MapReduce
最近终于抽出时间开始学习MIT 6.824,本文为我看MapReduce论文和做lab后的总结. [MapReduce英文论文] lab要用到go语言,这也是我第一次接触.可以参考go语言圣经学习基本 ...
- thinkphp添加excel更新数据表数据(优化篇)
由于主管说使用saveAll更新数据效率太低,要改用sql语句一次执行现在修改 /** * excel开启上传 * author: panzhide * @return array * Date: 2 ...
- Java基础 Java-IO流 深入浅出
建议阅读 重要性由高到低 Java基础-3 吃透Java IO:字节流.字符流.缓冲流 廖雪峰Java IO Java-IO流 JAVA设计模式初探之装饰者模式 为什么我觉得 Java 的 IO 很复 ...
- Qt开发技术:图形视图框架(一)基本介绍
前话 使用到Qt的视图框架. Qt视图框架介绍 简介 图形视图框架(The Graphic View Framework)用于管理和与大量定制的二维图形项目交互,以及用于可视化项目的视图小 ...
- Day11_50_SortedMap集合
SortedMap集合 二叉查找树 和 二叉*衡树 二叉查找树是一种有序的树,所有的左孩子的value值都是小于叶子结点的value值的,所有右孩子的value值都是大于叶子结点的.这样做的好处在于: ...
- js的13种继承
js继承的13种方式 也可以说只有12种,ES6的extend 也是12种方法之一-寄生继承的语法糖 1.原型链法 代码示例 Child.prototype = new Parent(); 所属模式: ...
- Firefox 启动带有配置信息
若不设置进行下述配置,那么 webdriver 每次启动火狐浏览器,默认都是一个不太有任何插件的浏览器被启动. 通过配置的方式,指定一个浏览器设置来启动,就可以使用以前安装的插件或配置信息了. 步骤一 ...
- 使用docker快速安装软件
安装mysql mkdir /opt/mysql /opt/mysql/etc /opt/mysql/data docker run -itd --name mariadb -e MYSQL_ROOT ...
- html书签展示(带搜索)
源代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title ...