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. ios动画效果集锦(持续更新)

    1.树叶滚动进度:http://www.jianshu.com/p/800496caa055 2.列表滚动动画和滚动视差效果http://www.jianshu.com/p/42e1eb59a1af ...

  2. Markdown语法图解

    Markdown语法图解 文章目录 快捷键 基本语法 对字体设置斜体.粗体.删除线 分级标题 链接 分割线 代码块 引用 列表 表格 常用技巧 换行 缩进字符 如何打出一些特殊符号 字体.字号与颜色 ...

  3. [SDOI2015][bzoj3994] 约数个数和 [莫比乌斯反演]

    题面: 传送门 思路: 首先,我们需要证明一个结论:d(i*j)等于sigma(gcd(x,y)==1),其中x为i的约数,y为j的约数 对于nm的每一个质因子pi分别考虑,设n = pi^ai + ...

  4. BZOJ 4069 [Apio2015]巴厘岛的雕塑 ——贪心

    自己首先想了一种方法$f(i)$表示前$i$个最小值为多少. 然而发现位运算并不满足局部最优性. 然后我们可以从高到低贪心的判断,使得每一组的和在一个特定的范围之内. 还要特判最后一个Subtask, ...

  5. qw

    // 主页 @RequestMapping(value = "/home") public ModelAndView home() { ModelAndView MV = new ...

  6. python 小爬虫

    import reimport urllibdef getHtml(url): page=urllib.urlopen(url); html=page.read() return htmldef ge ...

  7. String 类详解

    StringBuilder与StringBuffer的功能基本相同,不同之处在于StringBuilder是非线程安全的,而StringBuffer是线程安全的,因此效率上StringBuilder类 ...

  8. Xcode 真机调试报错:This application's application-identifier entitleme

        This application's application-identifier entitlement does not match that of the installed appli ...

  9. Java中Collections的sort方法和Comparable与Comparator的比较

    一.Comparable 新建Student1类,类实现Comparable接口,并重写compareTo方法 public class Student1 implements Comparable& ...

  10. 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---34

    以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下: