257. Binary Tree Paths

Total Accepted: 29282 Total
Submissions: 113527 Difficulty: Easy

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

分析:

这个算法写的太精妙了,參考讨论区!

/**
* 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:
void getDfsPaths(vector<string>& result, TreeNode* node, string strpath) {
if(!node->left && !node->right){//叶子
result.push_back(strpath);
return ;
}
if(node->left)
getDfsPaths(result, node->left, strpath+"->"+to_string(node->left->val));
if(node->right)
getDfsPaths(result, node->right, strpath+"->"+to_string(node->right->val));
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> ret;
if(!root)
return ret; getDfsPaths(ret, root, to_string(root->val));
return ret;
}
};

小结:

1。深度搜索应该立马条件反射,採用前序式遍历(假设用递归的话)

2,深度优先搜索应该立马联想到栈来实现迭代

3,递归具有保存变量信息的功能,有时候值得利用

联动第二十二题

【1】 22. Generate Parentheses。http://blog.csdn.net/ebowtang/article/details/50557414

注:本博文为EbowTang原创,兴许可能继续更新本文。假设转载,请务必复制本条信息!

原文地址:http://blog.csdn.net/ebowtang/article/details/50493936

原作者博客:http://blog.csdn.net/ebowtang

&lt;LeetCode OJ&gt; 257. Binary Tree Paths的更多相关文章

  1. [LeetCode&Python] Problem 257. Binary Tree Paths

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

  2. 【leetcode❤python】 257. Binary Tree Paths

    深度优先搜索 # Definition for a binary tree node.# class TreeNode:#     def __init__(self, x):#         se ...

  3. 【LeetCode】257. Binary Tree Paths

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

  4. LeetCode OJ 257. Binary Tree Paths

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

  5. LeetCode 257. Binary Tree Paths (二叉树路径)

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

  6. 【一天一道LeetCode】#257. Binary Tree Paths

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

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

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

  8. 【LeetCode】257. Binary Tree Paths 解题报告(java & python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  9. Leetcode 257. Binary Tree Paths

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

随机推荐

  1. C#知识点<4>

    1\C# 运算符重载 您可以重定义或重载 C# 中内置的运算符.因此,程序员也可以使用用户自定义类型的运算符.重载运算符是具有特殊名称的函数,是通过关键字 operator 后跟运算符的符号来定义的. ...

  2. iptables端口转发命令

    需求很简单,把本地81端口映射到8080端口上 1. 所有的81请求转发到了8080上.   1 # iptables -t nat -A PREROUTING -p tcp --dport 81 - ...

  3. Mysql存储过程从0开始(上)

    1.首先你要明白,mysql也是一种语言,他也可以编写程序,也是支持逻辑判断,if,elseif,else,switch,while等等的判断 2.mysql赋值一个变量的值操作:set @a = 1 ...

  4. [SPOJ839]Optimal Marks

    [SPOJ839]Optimal Marks 试题描述 You are given an undirected graph \(G(V, E)\). Each vertex has a mark wh ...

  5. [AHOI2017/HNOI2017][bzoj4827] 礼物 [FFT]

    题面 传送门 思路 首先,有一个结论:两个手环增加非负整数亮度,等于其中一个增加一个整数亮度(可以为负) 我们令增加量为$x$,旋转以后的原数列为${a}{b}$那么现在的费用就是: $\sum_{i ...

  6. Linux硬链接和软链接(符号链接)

    硬链接与软连接 :https://blog.csdn.net/u013777351/article/details/50557260 索引节点:https://blog.csdn.net/jessey ...

  7. redux使用需要注意的地方

    1. react和redux没有直接联系,当react需要结合redux使用的时候,需要引入 react-redux ,该插件提供了connet等方法使得react可以注入redux属性. 2. re ...

  8. 解决jsp在ios小屏手机下面滑动不流畅的问题

    今天做好的静态文件发给后台改成jsp之后,发现原本流畅滑动的页面在iphone5下面变得一卡一卡的. 之后加上了 -webkit-overflow-scrolling: touch; 这个属性之后,成 ...

  9. VMware Esxi5.5中嵌套虚拟机的网络设置方法

    环境: Esxi5.5服务器->虚拟机(WinServer2008R2)->VMware WorkStation(Win7虚拟机) 网络问题: VMware WorkStation中的虚拟 ...

  10. Javascript&Html-延迟调用和间歇调用

    Javascript&Html-延迟调用和间歇调用 Javascript 是一种单线程语言,所有的javascript任务都会放到一个任务列表中,这些javascript任务会按照插入到列表中 ...