题意:  

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

思路:

  直接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. git在公司内部的使用实践(转)

    从2011.10月左右,开始在后台组推行git版本控制,到现在也差不多半年了,也形成了一套基于git flow的副官模式工作流程: 版本定义: 版本号使用x.x.x进行定义,第一个x代表大版本只有在项 ...

  2. event.srcElement在火狐(FireFox)下的兼容问题。搜索框获得焦点时默认文字变化

    前言: 项目中用到了一个功能,搜索框里有默认的文字,当搜索框获得焦点时里面的默认文字消失,如果失去焦点时搜索框内容为空则让里面的内容回复默认!,. 实现: 很轻松的在网上找到了类似代码 $(" ...

  3. uva 1210

    #include<iostream> #include<cstring> using namespace std; + ; bool notprime[MAXN];//值为fa ...

  4. TopCoder SRM 583 TurnOnLamps

    读错题了有没有呀,原来 lamps 是在边上的呀,当成是在点上的了,无语. 直接一个dfs 就可以 从叶子节点开始,如果有必要转换 lamp 的状态则加一个仅包含 这个 lamp 的段 然后向上扩展, ...

  5. 有关嵌入式linux的注意点总结

    知识收集和个人学习过程遇到的问题. 仅供参考. 1.sudo apt-get update 一直无法更新 一,查看网络是否连接上 有几种网络连接方式.常用的两种有网桥网络(Bridged)和网络地址翻 ...

  6. Opencv的基础结构与内容

  7. raspbian 静态IP

    edit file:  /etc/network/interfaces change line: iface eth0 inet dhcp iface eth0 inet static address ...

  8. main函数参数的使用

    int main(int argc, char * argv[]) argc: argument count argv:argument vector 其中, char * argv[] 指针数组 c ...

  9. NSString的几种常用方法

    NSString的几种常用方法   要把 “2011-11-29” 改写成 “2011/11/29”一开始想用ios的时间格式,后来用NSString的方法搞定. [string stringByRe ...

  10. form表单select联动

    下拉列表:二级联动菜单 Select对象的常用属性 options[]:返回所有option组成的一个数组: name:名称 value:option的value的值 length:设置或读取opti ...