一天一道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. 2015 多校联赛 ——HDU5316(线段树)

    Fantasy magicians usually gain their ability through one of three usual methods: possessing it as an ...

  2. Codeforces Round #438 C. Qualification Rounds

    Description Snark and Philip are preparing the problemset for the upcoming pre-qualification round f ...

  3. 【吃炸弹的鸽子UVA10765-双联通模板】

    ·从前有一个鸽子Lence,它吃了一个炸弹,然后有人出了这道题. ·英文题,述大意:        给出一张连通无向图,求出:对于每个点,删去这个点(以及它相连的边以后)时,当前图中的连通块数量,这个 ...

  4. 文本分类学习 (五) 机器学习SVM的前奏-特征提取(卡方检验续集)

    前言: 上一篇比较详细的介绍了卡方检验和卡方分布.这篇我们就实际操刀,找到一些训练集,正所谓纸上得来终觉浅,绝知此事要躬行.然而我在躬行的时候,发现了卡方检验对于文本分类来说应该把公式再变形一般,那样 ...

  5. What's New In MySQL 8.0

        由于8.0内有很多C++11特性.需要gcc4.8版本以上.Rhel6系列默认gcc是4.7.在安装gcc6.1之后仍然检查不过. 原因可能是6.1版本不一定高于4.7,暂不讨论.鉴于升级gc ...

  6. hibernate实体对象的三种状态:自由状态,持久状态,游离状态.

    自由态与游离态的区别: 当一个持久化对象,脱离开Hibernate的缓存管理后,它就处于游离状态,游离对象和自由对象的最大区别在于,游离对象在数据库中可能还存在一条与它 对应的记录,只是现在这个游离对 ...

  7. oss web直传

    签名信息 auth.php <?php function gmt_iso8601($time) { $dtStr = date("c", $time); $mydatetim ...

  8. nodeppt的使用教程

    为什么选择nodeppt 这可能是迄今为止最好的网页版演示库 基于GFM的markdown语法编写 支持html混排,再复杂的demo也可以做! 支持多个皮肤:colors-moon-blue-dar ...

  9. 动手实现一个vue中的模态对话框组件

    写在前面 对话框是很常用的组件 , 在很多地方都会用到,一般我们可以使用自带的alert来弹出对话框,但是假如是设计 出的图该怎么办呢 ,所以我们需要自己写一个对话框,并且如果有很多地方都用到,那我们 ...

  10. 2016-wing的年度总结

    大神们都爱写总结,为了早日成为大神,我也来写一波. 2016 有很多事情发生. 从日常生活来讲,生活水平得到了一定提升,从600一个月的村子搬到了800一个月的村子(/捂脸); 从就业环境来讲,许多人 ...