题意:  

  给出一个二叉树,输出根到所有叶子节点的路径。

思路:

  直接DFS一次,只需要判断是否到达了叶子,是就收集答案。

 /**
* 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 {
vector<string> ans;
public:
void DFS(string path,TreeNode* t)
{
if(t->left==NULL&&t->right==NULL)
{
ans.push_back(path);
return ;
} if(t->left)
DFS(path+"->"+to_string(t->left->val),t->left);
if(t->right)
DFS(path+"->"+to_string(t->right->val),t->right);
}
vector<string> binaryTreePaths(TreeNode* root) {
if(root!=NULL) DFS(to_string(root->val),root);
return ans;
}
};

AC代码

LeetCode Binary Tree Paths(简单题)的更多相关文章

  1. [LeetCode] Binary Tree Paths 二叉树路径

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

  2. leetcode : Binary Tree Paths

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

  3. LeetCode——Binary Tree Paths

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

  4. Python3解leetcode Binary Tree Paths

    问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...

  5. LEETCODE —— Binary Tree的3 题 —— 3种非Recursive遍历

    Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' valu ...

  6. leetcode Binary Tree Paths python

    # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...

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

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

  8. 【LeetCode】257. Binary Tree Paths

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

  9. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

随机推荐

  1. Storm(3) - Calculating Term Importance with Trident

    Creating a URL stream using a Twitter filter Start by creating the project directory and standard Ma ...

  2. bzoj 2245: [SDOI2011]工作安排

    #include<cstdio> #include<iostream> #include<cstring> #define M 10000 #define inf ...

  3. flash builder的编译缓存

    C:\Users\Administrator\AppData\Roaming 因为我的一个项目是手机.浏览器都支持的项目,所以我经常删除项目然后修改成别的类型: 可能是这个原因,导致我的程序或者加载的 ...

  4. hdu 4627 The Unsolvable Problem

    http://acm.hdu.edu.cn/showproblem.php?pid=4627 分类讨论一下就可以 代码: #include<iostream> #include<cs ...

  5. Rsync+sersync文件实时同步

    一.为什么要用Rsync+sersync架构1.sersync是基于Inotify开发的,类似于Inotify-tools的工具2.sersync可以记录下被监听目录中发生变化的(包括增加.删除.修改 ...

  6. JAVA中StringBuffer类常用方法详解

    String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...

  7. 戴文的Linux内核专题:01介绍

    转自Linux中国 译者按:本文作者戴文.科利尔.约翰逊(Devyn Collier Johnson)今年才19岁,但是他在Linux内核.人工智能.编程语言方面拥有丰富的经验,本文是其在linux. ...

  8. HDU 4548

    题意: 给你一个区间,问你这个区间中的数既是素数又是美素数的数有多少个?美素数:首先这个数本身必须是素数并且它的各位数字的和也是素数; 如29,本身是素数, 而且2+9 = 11也是素数, 所以它是美 ...

  9. Allow windows service to "Interact with desktop"

    Typically, services are designed to run unattended without any UI with any need to interact with des ...

  10. 上架app 到app store 的出现: “The IPA is invalid. It does not inlude a Payload directory.”错误处理

    今天打包上传app到app store上遇到的一个错误,在xcode6.2下提示: The IPA is  invalid. It does not inlude a Payload director ...