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 ...
随机推荐
- Levenshtein Distance算法(编辑距离算法)
编辑距离 编辑距离(Edit Distance),又称Levenshtein距离,是指两个字串之间,由一个转成另一个所需的最少编辑操作次数.许可的编辑操作包括将一个字符替换成另一个字符,插入一个字符, ...
- 浅谈SQL Server中的三种物理连接操作
简介 在SQL Server中,我们所常见的表与表之间的Inner Join,Outer Join都会被执行引擎根据所选的列,数据上是否有索引,所选数据的选择性转化为Loop Join,Merge J ...
- java Io缓冲区复制文件
//直接写代码了 类和包自己导入创建 ,手打很累的 public static void main(String args[]){ FileReader fr=null; //文件输入流 Fil ...
- php实现设计模式之 访问者模式
<?php /** * 访问者模式 * 封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作. * 行为类模式 */ /** 抽象访问者:抽象类或 ...
- Code First :使用Entity. Framework编程(5) ----转发 收藏
第五章 对数据库映射使用默认规则与配置 到目前为止我们已经领略了Code First的默认规则与配置对属性.类间关系的影响.在这两个领域内,Code First不仅影响模型也影响数据库.在这一章,你将 ...
- SQL Server 2012提供的OFFSET/FETCH NEXT与Row_Number()对比测试(转)
原文地址:http://www.cnblogs.com/downmoon/archive/2012/04/19/2456451.html 在<SQL Server 2012服务端使用OFFSET ...
- jQuery动画特效实例教程
本文以实例形式详细讲述了jQuery动画特效的实现方法. 1.自制折叠内容块 内容块如下: <div class="module"> <div cla ...
- css判断不同分辨率显示不同宽度布局实现自适应宽度
一.CSS DIV网页布局中当分辨率小于等于1024px(像素)时,DIV布局对象显示1000px宽度,当分辨率大于1024px时候显示1200px宽度等需求.使用CSS实现改变浏览器显示宽度从而实现 ...
- HTML块级标签汇总(小篇)
块级元素,简单来说,就是自己独占一行的元素.其特点: ①总是在新行上开始: ②高度,行高以及外边距和内边距都可控制: ③宽度缺省是它的容器的100%,除非设定一个宽度. ④它可以容纳内联元素和其他块元 ...
- SAP中需要记住的一些标准表
E070 请求号的抬头表 E071 请求号的行项目表 E07T 请求号的文本 E71K TASK的抬头(子请求) ******************************************* ...