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 ...
随机推荐
- Android开发之重力传感器
重力传感器与方向传感器的开发步骤类似,只要理清了期中的x,y,z的值之后就可以根据他们的变化来进行编程了,首先来看一副图 假设当地的重力加速度值为g 当手机正面朝上的时候,z的值为q,反面朝上的时候, ...
- XmlReader和XElement组合之读取大型xml文档
简介 在.NET framework 中存在大量操作xml数据的类库和api,但在.NET framework 3.5后我们的首选一般就是linq to xml. linq to xml操作xml数据 ...
- Java程序,基本数据类型、、数据类型转换、变量和常量、常用运算符
一.基本数据类型 整数类型:byte.short. int(常用).long(较常用) 定义某个变量 int a=10: 浮点类型(小数):float.double(常用) ...
- PHP流程控制结构之分支结构
流程控制对于任何一门编程语言来说都是具有通用与普遍性的,是程序的重要组成部分.可以这么说,在任何一门程序设计语言中,都需要支持三种基本结构:顺序结构.分支结构(选择结构或条件结构)和循环结构.对于顺序 ...
- Three.js 入门指南(核心对象)
推荐大家可以看看这个:http://wenku.baidu.com/link?url=RQU2exzV_EF3GATc3bzQU2o9LGMuCmiN5nUJth5SLG3E2TrxtBLQodJU_ ...
- Android开发4: Notification编程基础、Broadcast的使用及其静态注册、动态注册方式
前言 啦啦啦~(博主每次开篇都要卖个萌,大家是不是都厌倦了呢~) 本篇博文希望帮助大家掌握 Broadcast 编程基础,实现动态注册 Broadcast 和静态注册 Broadcast 的方式以及学 ...
- Sharepoint学习笔记—ECM系列—文档列表的Metedata Navigation与Key Filter功能的实现
如果一个文档列表中存放了成百上千的文档,想要快速的找到你想要的还真不是件容易的事,Sharepoint提供了Metedata Navigation与Key Filter功能可以帮助我们快速的过滤和定位 ...
- Android M Permission 运行时权限 学习笔记
Android M Permission 运行时权限 学习笔记 从Android 6.0开始, 用户需要在运行时请求权限, 本文对运行时权限的申请和处理进行介绍, 并讨论了使用运行时权限时新老版本的一 ...
- UIScrollView的delegate方法妙用之让UICollectionView滑动到某个你想要的位置
一个UICollectionView有好多个cell,滑动一下,谁也不知道会停留在哪个cell,滑的快一点,就会多滑一段距离,反之则会滑的比较近,这正是UIScrollview用户体验好的地方. 如果 ...
- 【代码笔记】iOS-通过颜色来生成一个纯色图片
一,效果图. 二,代码. RootViewController.m - (void)viewDidLoad { [super viewDidLoad]; // Do any additional se ...