<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 ...
随机推荐
- JS判断是否是IE浏览器的几种方式
1.得到浏览器的信息,返回由客户机发送服务器的 user-agent 头部的值. if(navigator.userAgent.indexOf("MSIE 8.0")>0){ ...
- 截图神器-snipaste
基础操作 Snipaste 是一个简单但强大的贴图工具,同时也可以执行截屏.标注等功能. 截屏 开始截图 快捷键(默认为 F1) 鼠标左键 单击托盘图标 何谓一次 成功的截图 保存到剪贴板 ( / ...
- 人类基因(human)
题目描述 上了大学之后,小W和小Z一起报了一门三宝课,在实践课上遇到了一些问题. 一条染色体上有nn对基因,每种基因都可以用一个数ai来表示,作为标号. 现在,在其中一条染色单体上某基因发生了突变. ...
- git 使用报错记录
错误一:git fatal: unable to write new index file主要原因就是服务器磁盘空间不够导致的,增加服务器空间就OK了在百度上面搜索没得到什么有效信息,在gooogle ...
- Docker 开源项目之 registry - 部署 registry (注册表)服务器
原文地址 在部署 registry 之前需要现在主机上安装 Docker.registry 实际上就是运行在 Docker 中的 registry 镜像的实例. 本主题提供关于部署和配置 regist ...
- FckEditor2.65使用范例源码
原文发布时间为:2009-10-12 -- 来源于本人的百度文章 [由搬家工具导入] 下载地址:http://www.xmaspx.com/Services/FileAttachment.ashx?A ...
- [论文]CA-Tree: A Hierarchical Structure for Efficient and Scalable Coassociation-Based Cluster Ensembles
作者:Tsaipei Wang, Member, IEEE 发表:IEEE TRANSACTIONS ON SYSTEMS, MAN, AND CYBERNETICS—PART B: CYBERNET ...
- SaltStack 模块学习之拷贝master服务器上文件和目录到minion服务器
一. cp.get_file实现从master端复制文件到minion服务器的文件中cp.get_file 1. 修改/etc/salt/master ,指定server 工作的根目录 file- ...
- 【原创】打开Excel时提示"您尝试打开的文件**.xls的格式与文件扩展名指定的格式不一致"
问题描述: 系统安装了WPS时,Analyzer导出excel时候,会提示"您尝试打开的文件**.xls的格式与文件扩展名指定的格式不一致",这是Excel的安全问题, ...
- html5手机返回按钮跳转到指定页面问题
最近在做活动的时候有一个这样的场景,在主页面点击跳出一个弹层表单,填写完信息后,点击确认跳转到指定的展示页面了.这时候在手机端点击浏览器自带的返回按钮后,回到主页面,这时候主页面无法刷新,弹层信息还在 ...