一天一道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. [APIO2016]

    2016的题貌似是韩国棒子出的,好丧啊.... 看了题解还想了好久...... ------------------------------------------------- A.Boat 有n个 ...

  2. linux 下文件误删恢复

    linux 下文件误删恢复 0x01 事件背景 某天晚上写代码的时候,本来想删除当前目录下一个叫xxx的文件夹 rm -rdf ./xxx/*, 结果光顾着和人说话,一不留神手贱把命令敲成了rm -r ...

  3. java版的类似飞秋的局域网在线聊天项目

    原文链接:http://www.cnblogs.com/wangleiblog/articles/5323305.html 转载请注明 最近在弄一个java版的局域网在线聊天项目,功能跟飞秋差不多.p ...

  4. face-alignment:用 pytorch 实现的 2D 和 3D 人脸对齐库

    使用世界上最准确的面对齐网络从 Python 检测面部地标,能够在2D和3D坐标中检测点. 项目地址:https://github.com/1adrianb/face-alignment 作者: 阿德 ...

  5. iOS 搜索记录

    需求描述: 使用单独的搜索界面, 提供用户进行搜索并留下搜索记录. 搜索记录可以提供用户进行再次搜索, 或者把搜索记录清空. 方案和技术点: 存储方式使用 NSUserDefaults, 把对应的字段 ...

  6. jquery 引号问题

    varFrozenColumns="[[{'field':'CZ','title':'操作','width':80,'align':'center','formatter':function ...

  7. Jenkins持续集成环境, 如何自定义 maven 仓库

    最后的解决方法: job-->configure-->Build-->Goals and options: clean package -Dmaven.repo.local=D:\d ...

  8. node的异常处理

    Node是单线程运行环境,一旦抛出的异常没有被捕获,就会引起整个进程的崩溃.所以,Node的异常处理对于保证系统的稳定运行非常重要. node的处理方法: 1.使用throw语句抛出异常 常用的捕获异 ...

  9. Junit4 java.lang.Exception: No runnable methods

    出现如下错误: java.lang.Exception: No runnable methods at org.junit.runners.BlockJUnit4ClassRunner.validat ...

  10. POJ 3050 Hopscotch DFS

    The cows play the child's game of hopscotch in a non-traditional way. Instead of a linear set of num ...