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 ...
随机推荐
- 趣味python编程之经典俄罗斯方块
国庆期间闲不住,用python把经典俄罗斯方块实现了一遍,找到了些儿时的乐趣.因此突发奇想,打算用python写点经典又确实有趣的小程序形成系列.正统编程之余也给自己找点儿乐趣,换个角度写程序. 原计 ...
- node.js 的事件驱动
events 模块只提供了一个对象: events.EventEmitter. EventEmitter 的核心就是事件发射与事件监听器功能的封装.EventEmitter 的每个事件由一个事件名和若 ...
- Create function through MySQLdb
http://stackoverflow.com/questions/745538/create-function-through-mysqldb How can I define a multi-s ...
- 【转】ZigBee是如何组网的?
组网方案设计:组建一个完整的zigbee网状网络包括两个步骤:网络初始化.节点(路由器或终端)加入网络,其中节点加入网络又包括两个步骤:通过与协调器连接入网和通过已有父节点入网. 一.网络初始化: ...
- C++_系列自学课程_第_5_课_vector容器_《C++ Primer 第四版》
再一次遇到 vector 这个单词; 每一次见到这个单词都感觉这个单词非常的 "高大上"; 数字遇到vector马上就可以360度旋转: 当 "电" 遇到vec ...
- 从零开始学 Java - 利用 Nginx 负载均衡实现 Web 服务器更新不影响访问
还记得那些美妙的夜晚吗 你洗洗打算看一个小电影就睡了,这个时候突然想起来今天晚上是服务器更新的日子,你要在凌晨时分去把最新的代码更新到服务器,以保证明天大家一觉醒来打开网站,发现昨天的 Bug 都不见 ...
- 变通实现微服务的per request以提高IO效率
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- js正则表达式图形化工具-rline
github地址:https://github.com/finance-sh/rline 在线demo: http://lihuazhai.com/demo/test.html 这是一个js正则表达式 ...
- 使用setTimeout模拟setInterval效果
由于现在部分浏览器基于对系统性能的优化,在使用setInterval的时候,在页面没有获得关注的状态,浏览器可以会自动将setInterval终端,等到该页面重新获得关注时再开启.这样就会使得一些基于 ...
- 转发:Chrome 控制台console的用法
大家都有用过各种类型的浏览器,每种浏览器都有自己的特色,本人拙见,在我用过的浏览器当中,我是最喜欢Chrome的,因为它对于调试脚本及前端设计调试都有它比其它浏览器有过之而无不及的地方.可能大家对co ...