要求

  • 给定一棵二叉树,返回所有表示从根节点到叶子节点路径的字符串

示例

  • ["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的更多相关文章

  1. Leetcode题 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  2. &lt;LeetCode OJ&gt; 257. Binary Tree Paths

    257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...

  3. 【LeetCode】257. Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...

  4. 257. Binary Tree Paths返回所有深度优先的遍历

    [抄题]: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

  5. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  6. 257. Binary Tree Paths

    题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree ...

  7. Leetcode 257. Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  8. (easy)LeetCode 257.Binary Tree Paths

    Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...

  9. Java [Leetcode 257]Binary Tree Paths

    题目描述: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tr ...

随机推荐

  1. Java中的集合Set - 入门篇

    前言 大家好啊,我是汤圆,今天给大家带来的是<Java中的集合Set - 入门篇>,希望对大家有帮助,谢谢 简介 前面介绍了集合List,映射Map,最后再简单介绍下集合Set,相关类如下 ...

  2. python基础(十七):函数

    在正式讲述函数之前,先给大家说明一点:编写函数就是"面向过程"的方式,编写类就是"面向对象"的方式.你如果不知道这是啥意思,至少别人提到这2个词你应该知道是在干 ...

  3. c++排序相关的参数“cmp“的用法及理解

    对sort函数(需要algorithm头文件),它的cmp可以是"函数",也可以是"对象" bool myfunction (int i,int j) { re ...

  4. 单个java文件打成可执行jar包

    1 概述 使用JDK自带的jar与java将单个java文件打成可执行jar包并运行. 当然也可以使用IDE完成,使用Maven只需要一个简单的package,但是单个文件嘛,没必要这么"凶 ...

  5. User-Agent大全 python

    1 # -*-coding:utf-8 -*- 2 3 import random 4 5 # 返回一个随机的请求头 headers 6 def getheaders(): 7 # 各种PC端 8 u ...

  6. ElasticSearch-02-elasticsearch.yaml

    # ======================== Elasticsearch Configuration ========================= # # NOTE: Elasticse ...

  7. 有了CopyOnWrite为何又要有ReadWriteLock?

    引言 前文我们有介绍<看了CopyOnWriteArrayList后自己实现了一个CopyOnWriteHashMap> 关于CopyOnWrite容器的,但是它也有一些缺点: 内存占用问 ...

  8. Leecode第二题:两数相加

    Leecode2 先看题目 : 给你两个 非空 的链表,表示两个非负的整数.它们每位数字都是按照 逆序 的方式存储的,并且每个节点只能存储 一位 数字. 请你将两个数相加,并以相同形式返回一个表示和的 ...

  9. 1151 LCA in a Binary Tree (30point(s))

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  10. Ubuntu20.04安装MongoDB

    本教程描述了如何在Ubuntu20.04上安装MongoDB4.4 安装MongoDB Ubuntu 20.04默认存储库中不提供最新版本的MongoDB,因此需要在系统中添加官方的MongoDB存储 ...