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. spring IOC注解方式详解

    本文分为三个部分:概述.使用注解进行属性注入.使用注解进行Bean的自动定义. 一,概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以 ...

  2. 浏览器不支持JavaScript怎么办

    使用  noscript 标签,给用户提醒即可 <body> <noscript>需要js支持</noscript> </body>

  3. Socket通信入门小实例

    客户端: public class Client { private int port = 8000; private String host = "localhost"; pri ...

  4. PHP网站提交表单如何实现验证码验证功能

    很多小伙伴在原生网站时候都会遇到很多意料之外的情况发生,也有不少小伙伴在初学程序代码的时候可能会因为一个小小的逗号隐藏在几百行,几千行的代码中出错常见的在提交表单中很多事需要验证码验证,那么如何来实现 ...

  5. 使用caffe测试自己的图片

    第一种方法是测试批量图片,使用caffe.bin即可,首先要做的是把你的jpg图片转换为LMDB的格式,如何转换呢?用/build/tools/convert_image --resize_width ...

  6. java连接adsl

    http://blog.csdn.net/qq_28784775/article/details/54134169#comments

  7. 微信小程序 报警告的解决办法

    wx:for   如果没有给它相应的  wx:key 控制台就会有警告,解决的办法给它添加相应的key警告就消失啦

  8. 30+ Excellent Windows Phone 7 Development Tutorials

    原文发布时间为:2012-01-16 -- 来源于本人的百度文章 [由搬家工具导入] Here are 30+ cool Windows Phone Development articles for ...

  9. VIM使用技巧4

    使移动和修改都能重复,对重复的操作能够回退比能够重复更加重要: 目的操作重复回退序号 执行修改{edit}.u1 在行内查找下一个指定字符 f{char}/t{char};,2 在行内查找上一个指定字 ...

  10. 域名解析系统DNS诊断命令nslookup详解【转】

    转自:http://www.renhaibo.com/archives/29.html Ping指令我们很熟悉了,它是一个检查网络状况的命令,在输入的参数是域名的情况下会通过DNS进行查询,但只能查询 ...