[刷题] 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 ...
随机推荐
- 去空格的四则运算表达式求值-Java
笔记 package com.daidai.day4.demo1; import java.util.ArrayList; import java.util.Arrays; import java.u ...
- k8s删除节点
k8s 删除节点 线上环境 # ctl get nodes NAME STATUS ROLES AGE VERSION 10.0.0.123 Ready <none> 104d v1.20 ...
- python基础(十一):集合的使用(下)
add() 含义:向集合中添加指定的元素: # 注:重复元素没办法加入. >>> s = {1,2,3} >>> s.add(2) >>> s{1 ...
- (一)Struts2框架概述
一.struts2发展历史 经过很多年发展,Struts1已经成为了高度成熟的框架,但随着时间的发展,Struts1的局限性和缺点不断的暴露出来. 现在Struts已经分化成了两个框架 ...
- SwiftUI 简明教程之文本与图片
本文为 Eul 样章,如果您喜欢,请移步 AppStore/Eul 查看更多内容. Eul 是一款 SwiftUI & Combine 教程类 App(iOS.macOS),以文章(文字.图片 ...
- 【笔记】《算法竞赛入门》习题7-3 UVa211_多米诺效应
title: 习题7-3 UVa211_多米诺效应 date: 2021-01-29 19:08:00 categories: 算法竞赛入门 tags: 数据结构 算法 UVa 题目: 使用28个多米 ...
- Android+Java Web+MySQL实现登录注册
1 前言&概述 这篇文章是基于此处文章的更新,更新了一些技术栈,更加贴近实际需要,以及修复了若干的错误. 这是一个前端Android+后端Java/Kotlin通过Servelt进行后台数据库 ...
- Spring Boot demo系列(八):Swagger
2021.2.24 更新 1 概述 Swagger主要用于生成API文档,本文演示了如何使用目前最新的OpenAPI3以及Swagger来进行接口文档的生成. 2 依赖 <dependency& ...
- Jmeter接口测试-MD5加密-请求验签(完整流程)
第一部分:先准备好Jmeter 1.在开始编写脚本之前,先要确保你的Jmeter能够正常运行.若你还没有安装Jmeter,可参考以下方法: A.Jmeter需要java运行环境,所以需要下载JDK,J ...
- Day01_10_Scanner 接收用户输入
Scanner函数 import java.util.Scanner; class ScannerTest { public static void main(String[] args){ Syst ...