leetcode : Binary Tree Paths
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"]
思路:用两个stack<TreeNode*> in , s;
in : 记录当前的路径 p , 和vector<int>path 相同,只不过一个记录的是 val ,一个记录点的指针。
s : 记录每个节点的 p->right
v2s : 将path转换称符合要求的string
/**
* 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:string v2s(vector<int>a){
const int len = a.size();
string re = "";
char *p = new char[];
for (int i = ; i<len; i++){
stringstream ss; // 需要包含头文件 #include<sstream>
string temp;
ss << a[i];
ss >> temp;
re = re + temp;
if (i != len - )
re = re + "->";
}
return re;
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
vector<string> re;
if (root == NULL) return re;
TreeNode * p = root;
stack<TreeNode*> in, s;
while (p != NULL){
if (p->left == NULL&&p->right == NULL){
in.push(p);
path.push_back(p->val);
re.push_back(v2s(path));
if (s.empty())
return re;
else {
// 通过while循环,查找下一个需要遍历的点
while (!in.empty() && !s.empty() && (in.top())->right != s.top()){
in.pop();
path.erase(path.end()-);
}
p = s.top();
s.pop();
}
}
else if (p->left == NULL&&p->right != NULL){
path.push_back(p->val);
in.push(p);
p = p->right;
}
else if (p->left != NULL&&p->right == NULL){
path.push_back(p->val);
in.push(p);
p = p->left;
}
else{
path.push_back(p->val);
in.push(p);
s.push(p->right);
p = p->left;
}
}
return re;
}
};
另一道相似题目
有一棵二叉树,树上每个点标有权值,权值各不相同,请设计一个算法算出权值最大的叶节点到权值最小的叶节点的距离。二叉树每条边的距离为1,一个节点经过多少条边到达另一个节点为这两个节点之间的距离。
给定二叉树的根节点root,请返回所求距离。
思路:只需要将保存的路径进行比较,相同的去掉然后相加,就能算出路径长度。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Tree {
public:
int getDis(TreeNode* root) {
// write code here
stack<TreeNode*> max, min;
int m = INT_MIN, n = INT_MAX, re = 0;
stack<TreeNode*> in, s;
TreeNode *p = root;
while (p != NULL){
if (p->left == NULL&&p->right == NULL){
in.push(p);
if (p->val > m){
max = in;
m = p->val;
}
if (p->val < n){
min = in;
n = p->val;
}
while (!in.empty() && !s.empty() && (in.top())->right != s.top()){
in.pop();
}
if (s.empty())
break;
else {
p = s.top();
s.pop();
}
}
else if (p->left == NULL&&p->right != NULL){
in.push(p);
p = p->right;
}
else if (p->left != NULL&&p->right == NULL){
in.push(p);
p = p->left;
}
else {
in.push(p);
s.push(p->right);
p = p->left;
}
}
stack<TreeNode*> t1, t2;
while (!max.empty()){
t1.push(max.top());
max.pop();
}
while (!min.empty()){
t2.push(min.top());
min.pop();
}
while (!t1.empty() && !t2.empty()){
if (t1.top() != t2.top())
break;
else
t1.pop(); t2.pop();
}
re = re + t1.size() + t2.size();
return re;
}
};
leetcode : Binary Tree Paths的更多相关文章
- [LeetCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 ...
- LeetCode——Binary Tree Paths
Description: Given a binary tree, return all root-to-leaf paths. For example, given the following bi ...
- Python3解leetcode Binary Tree Paths
问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. E ...
- LeetCode Binary Tree Paths(简单题)
题意: 给出一个二叉树,输出根到所有叶子节点的路径. 思路: 直接DFS一次,只需要判断是否到达了叶子,是就收集答案. /** * Definition for a binary tree node. ...
- leetcode Binary Tree Paths python
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = ...
- 【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
257. Binary Tree Paths Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy Given a bina ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LeetCode_257. Binary Tree Paths
257. Binary Tree Paths Easy Given a binary tree, return all root-to-leaf paths. Note: A leaf is a no ...
随机推荐
- asp.net教程:编译错误同时存在于不同dll中
asp.net 编译错误类型“同时存在于”不同的dll中. 出现这种错误大概有三种情况: 1.ASPX页面,一个*.ASPX,对应着一个*.cs文件,两者其实是一个文件,通过两者实现代码分离,每个*. ...
- php实现设计模式之 观察者模式
代码片段一: <?php /** * 观察者模式:定于对象间的一种一对多的依赖关系,当一个对象发生改变时,所有依赖它的对象都收到通知并自动更新. */ //例子:少林方丈的通讯录,当扫地僧的号码 ...
- css3图片模糊过滤效果
css3图片过滤效果,鼠标放上后其它图片模糊,鼠标所在位置的图片是清淅的,有效索引出当前的图片,对图片的模糊处理是本特效的亮点,你完全可以将模糊的效果应用于其它的图片特效中,你同样也可借此代码研究一下 ...
- SharePoint 2013 新建网站集图解
前言:接触SharePoint的人可能是越来越多,但是很多人一接触就很迷茫,在技术群里问如何新建网站集,这样一篇图解,帮助新手学习在搭建好SharePoint环境之后,如何创建一个网站集,做一个基本的 ...
- 把Sharepoint Desinger 工作流部署到生产环境
下面是比较简单的方法,把Designer工作流从开发环境部署到生产环境. 在Sharepoint Desinger 2013 中点击需要部署的工作流. 点击保存,发布. 点Export to Visi ...
- ImageLoader配合ImageSwitcher的使用
先在MyApplication中初始化ImageLoader initImageLoader(getApplicationContext()); /** * 初始化ImageLoader * 如果你经 ...
- iOS Swift-元组tuples(The Swift Programming Language)
iOS Swift-元组tuples(The Swift Programming Language) 什么是元组? 元组(tuples)是把多个值组合成一个复合值,元组内的值可以使任意类型,并不要求是 ...
- Android 应用程序集成Google 登录及二次封装
谷歌登录API: https://developers.google.com/identity/sign-in/android/ 1.注册并且登录google网站 https://accounts. ...
- js判断鼠标是否停止移动
本程序实现当鼠标在一个特定的div内悬停n秒时,判断出已经停止移动. 思路: 1.定义全局变量鼠标移动状态imouse,定时器timer.当鼠标在div内移动时,imouse值为1,相反静止时值为0: ...
- Windows on Device 项目实践 4 - 智能风扇制作
在前面的文章中,我们已经学习并且利用Intel Galileo开发板和Windows on Device制作了火焰报警器.感光灯和PWM调光灯.在这个项目中,我们来利用温度传感器和直流电机,完成一个简 ...