一天一道LeetCode

本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github

欢迎大家关注我的新浪微博,我的新浪微博

欢迎转载,转载请注明出处

(一)题目

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”]

(二)解题

题目大意:给定一个二叉树,输出所有根节点到叶子节点的路径。

解题思路:采用深度优先搜索,碰到叶子节点就输出该条路径。

需要注意以下几点(也是我在解题过程中犯的错误):

  1. 需要考虑节点值为负数的情况,要转成string
  2. 要按照题目给定的格式来输出。

    下面看具体代码:
/**
 * 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:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> ret;
        string tmp;
        if(root!=NULL) dfsTreePaths(root,ret,tmp);
        return ret;
    }
    void dfsTreePaths(TreeNode* root,vector<string>& ret, string tmp)
    {
        if(root->left == NULL&& root->right==NULL) {//如果为叶子节点就输出
            char temp[10];
            sprintf(temp, "%d", root->val);//将整数转换成string
            tmp += string(temp);
            ret.push_back(tmp);
            return;
        }
        char temp[10];
        sprintf(temp, "%d", root->val);//将整数转换成string
        tmp += string(temp);
        tmp +="->";
        if(root->left !=NULL) dfsTreePaths(root->left,ret,tmp);//继续搜索左子树
        if(root->right !=NULL) dfsTreePaths(root->right,ret,tmp);//继续搜索右子树
    }
};

【一天一道LeetCode】#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. [LeetCode] 257. Binary Tree Paths 二叉树路径

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

  3. Leetcode 257. Binary Tree Paths

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

  4. (easy)LeetCode 257.Binary Tree Paths

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

  5. Java [Leetcode 257]Binary Tree Paths

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

  6. [leetcode]257. Binary Tree Paths二叉树路径

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  7. LeetCode 257. Binary Tree Paths(二叉树根到叶子的全部路径)

    Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example ...

  8. Leetcode 257 Binary Tree Paths 二叉树 DFS

    找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. /** * Definition for a binary tree node. * struct TreeNode { * i ...

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

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

  10. 【LeetCode】257. Binary Tree Paths

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

随机推荐

  1. WiFi天线分集

    0 概述 在调试一款古董级射频芯片时,发现它支持1发2收,由于在画板工程师将辅助天线也整出来.等板子贴出来后,就与同事一起折腾这个分集接收功能. 碰到过如下问题,先记录,以便后期有空再继续. 1)发现 ...

  2. 从零安装Scrapy心得 | Install Python Scrapy from scratch

    1. 介绍 Scrapy,是基于python的网络爬虫框架,它能从网络上爬下来信息,是data获取的一个好方式.于是想安装下看看. 进到它的官网,安装的介绍页面 https://docs.scrapy ...

  3. Spring MVC - 静态页面

    环境搭建 以下示例显示如何使用Spring MVC Framework编写一个简单的基于Web的应用程序,它可以使用<mvc:resources>标记访问静态页面和动态页面.首先使用Int ...

  4. C语言第三次程序设计作业

    (一)改错题 计算f(x)的值:输入实数x,计算并输出下列分段函数f(x)的值,输出时保留1位小数. 1)源程序(有错误的程序) #include <stdio.h> int main(v ...

  5. 解决nodejs中json序列化时Date类型为UTC格式

    在nodejs中,json序列化时Date类型时,默认转为UTC格式. 如下图 zhupengfei@DESKTOP-HJASOE3 MINGW64 /d/MyProject/exp2 $ node ...

  6. ios html5 audio 不能自动播放

    //修复ios 浏览器不能自动播放音频的问题 在加载时创建新的audio 用的时候更换src即可 Xut.fix = Xut.fix||{}; if (Xut.plat.isBrowser & ...

  7. C语言 字符串 字符串处理操作 字符串与函数

    字符数组的定义和初始化 宏常量+1  强调了字符串的最大强度 推荐忽略长度的定义 不能对所指向的存储单元内容修改,除非是字符串数组的定义 因为指针变量指向的是字符串数组的值,可以被修改. 未初始化 字 ...

  8. ubuntu查看IO

    在命令行直接 cp 一个比较大的文件时,由于没有提示信息,总感觉很不放心,可以通过查看IO的方式确认cp操作的进展程度. 查看IO可以使用iostat命令,但是前提是要安装sysstat sudo a ...

  9. python学习之路网络编程篇(第五篇)

    paramiko简介 paramiko 是基于Python实现的SSH2远程安装连接,支持认证及秘钥方式.可以实现远程命令执行.文件传输.中间SSH代理等功能. paramiko安装 #!/bin/b ...

  10. MySQL PHP 语法

    MySQL PHP 语法 MySQL 可应用于多种语言,包括 PERL, C, C++, JAVA 和 PHP. 在这些语言中,MySQL在PHP的web开发中是应用最广泛. 在本教程中我们大部分实例 ...