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 ...
随机推荐
- jQuery css3鼠标悬停图片显示遮罩层动画特效
jQuery css3鼠标悬停图片显示遮罩层动画特效 效果体验:http://hovertree.com/texiao/jquery/39/ 效果图: 源码下载:http://hovertree.co ...
- [moka同学笔记]四、Yii2.0课程笔记(魏曦老师教程)[匿名函数的使用操作]
在评论页面中index.php中 [ 'attribute'=>'status', 'value'=>'status0.name', 'filter'=>Commentstatus: ...
- Spring学习系列(三) 通过Java代码装配Bean
上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean 二.通过Java类装配bean 在前面定义了HelloWorldConfig类,并使用@Compon ...
- InteliJ Shortcuts
Open your browser with documentation for the element at the editor's caret Press Shift+F1 (View | Ex ...
- git的诞生
Git的诞生 很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了. Linus虽然创建了Linux,但Linux的壮大是靠全世 ...
- CSS3 float深入理解浮动资料整理
CSS浮动(float,clear)通俗讲解 CSS 浮动 CSS float浮动的深入研究.详解及拓展(一) CSS float浮动的深入研究.详解及拓展(二) 1.浮动实现图文环绕(理解难点) 浮 ...
- arcgis10.2.2桌面版具体的安装步骤过程
先声明一下,这里的截图虽说是ArcGIS10.1版本的,但是安装步骤是对的,本人用ArcGIS10.2.2软件测试成功安装上 一.ArcGIS许可证管理器安装 1.在软件包文件夹license man ...
- 安卓开发之activity详解(sumzom)
app中,一个activity通常是指的一个单独的屏幕,相当于网站里面的一个网页,它是对用户可见的,它上面可以显示一些控件,并且可以监听处理用户的时间做出响应. 那么activity之间如何进行通信呢 ...
- android handler传递消息机制
当工作线程给主线程发送消息时,因为主线程是有looper的,所以不需要初始化looper,注意给谁发消息就关联谁的handler,此时用的就是主线程的handler handler会把消息发送到Mes ...
- ListView和Adapter的配合使用以及Adapter的重写
ListView和Adapter的使用 首先介绍一下ListView是Android开发过程中较为常见的组件之一,它将数据以列表的形式展现出来.一般而言,一个ListView由以下三个元素组成: ...