<LeetCode OJ> 257. Binary Tree Paths
257. Binary Tree Paths
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
<LeetCode OJ> 257. Binary Tree Paths的更多相关文章
- [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 ...
- 【leetcode❤python】 257. Binary Tree Paths
深度优先搜索 # Definition for a binary tree node.# class TreeNode:# def __init__(self, x):# se ...
- 【LeetCode】257. Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. For example, given the followi ...
- LeetCode OJ 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode 257. Binary Tree Paths (二叉树路径)
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 【一天一道LeetCode】#257. Binary Tree Paths
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- [LeetCode] 257. Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- 【LeetCode】257. Binary Tree Paths 解题报告(java & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- Leetcode 257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
随机推荐
- PAT1026
要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间.这个时间单位是clock tick,即“时 ...
- HDU 4177 模拟时间问题
Avoiding a disaster Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- [cocos2dx UI] CCLabelAtlas 为什么不显示最后一个字
CClabelAtlas优点,基本用法等我就不说了,这里说一个和美术配合时的一个坑!就是图片的最后一位怎么也不显示,如下图中的冒号不会显示 查了ASCII码表,这个冒号的值为58,就是在9(57)的后 ...
- 【POJ3693】Maximum repetition substring (SA)
这是一道神奇的题目..论文里面说得不清楚,其实是这样...如果一个长度为l的串重复多次,那么至少s[1],s[l+1],s[2*l+1],..之中有相邻2个相等...设这时为j=i*l+1,k=j+l ...
- JS 监听绑定和取消事件
1. 原生 JS 语言: 绑定:addEventListener(type, function, false) 取消: removeEventListener(type, function, fals ...
- Http 请求头 Range
HTTP 请求头 Range 请求资源的部分内容(不包括响应头的大小),单位是byte,即字节,从0开始. 如果服务器能够正常响应的话,服务器会返回 206 Partial Content 的状态码及 ...
- Javascript&Html-延迟调用和间歇调用
Javascript&Html-延迟调用和间歇调用 Javascript 是一种单线程语言,所有的javascript任务都会放到一个任务列表中,这些javascript任务会按照插入到列表中 ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---45
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- 使用中科大源下载android源码
我的系统时manjaro linux 最新版的,安装过了git和curl软件 一.如果没有安装的同学,请使用命令: pacman -S git curl 我这里安装了python3和python2,但 ...
- 从零开始学习OpenCL开发(一)架构【转】
转自:http://blog.csdn.net/leonwei/article/details/8880012 多谢大家关注 转载本文请注明:http://blog.csdn.net/leonwei/ ...