94. Binary Tree Inorder Traversal(Tree, stack)
Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree [1,null,2,3],
1
\
2
/
3
return [1,3,2].
Note: Recursive solution is trivial, could you do it iteratively?
法I: recursion
/**
* 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:
vector<int> inorderTraversal(TreeNode* root) {
inorder(root);
return ret;
} void inorder(TreeNode* root){
if(root==NULL) return; inorder(root->left);
ret.push_back(root->val);
inorder(root->right);
return; }
private:
vector<int> ret;
};
法II:iteration
/**
* 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:
vector<int> inorderTraversal(TreeNode* root) {
if(root==NULL) return ret; TreeNode* current = root;
stack<TreeNode*> s;
s.push(current);
while(){
while(current->left){
s.push(current->left); //push left child
current = current->left;
flag.insert(current);
} //After iterate left tree, visit root
while(!s.empty() && s.top()->right == NULL){
current = s.top();
s.pop(); //pop root
ret.push_back(current->val); //visit root
}
if(s.empty()) break; //terminate when stack is empty
current = s.top();
s.pop(); //pop root
ret.push_back(current->val); //visit root //go to right child
s.push(current->right); //push right child
current = current->right;
} return ret;
} private:
vector<int> ret;
};
94. Binary Tree Inorder Traversal(Tree, stack)的更多相关文章
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator
144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- 【LeetCode】94. Binary Tree Inorder Traversal (3 solutions)
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- LintCode Binary Tree Inorder Traversal
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
- 37. Binary Tree Zigzag Level Order Traversal && Binary Tree Inorder Traversal
Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order traversa ...
- LeetCode: Binary Tree Inorder Traversal 解题报告
Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values ...
随机推荐
- springBoot整合MongoDB(单机)
依赖: <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-mon ...
- PHP 扩展在 Linux(centos7)系统下的编译与安装 以 mysqli 为例
(操作系统 Centos7,环境版本 php7) 01,进入到 PHP 解压后的源码包的的 ext 文件夹 02,查看是否存在 mysqli 扩展 => ls, 如果不存在需要去响应网站下载 ( ...
- WPF 中 TextBlock 文本换行与行间距
换行符: C#代码中:\r\n 或 \r 或 \n XAML中: 或 注:\r 回车 (carriage return 缩写),\n 新行 (new line 缩写). 行间距: LineHeigh ...
- day03-数据类型
数据类型 一.介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 mysql常用数据类型概括:#1. 数字: 整型:tinyint.int.bi ...
- Asp.net有三大对象:HttpContext, HttpRequest, HttpResponse
一.Asp.net有三大对象:HttpContext, HttpRequest, HttpResponse.命名空间: System.Web程序集: System.Web(在 System.Web ...
- iframe+form上传文件
<iframe id="iframe1" name="ifra1" style="display: none"></ifr ...
- 3Ds Max 2014
原文地址:https://blog.csdn.net/u011518678/article/details/50764835 1.3Ds Max 2014 的安装和激活 激活地址: https://j ...
- Mac中opencv批量对图片进行二值化
对灰度图像进行二值化,传入的图片是手写汉字的截图,通过二值化把字的部分提出来.用ostu进行二值化 #include <stdio.h> #include <iostream> ...
- Java的学习03
今天依然记录一下,学习情况,可以看到自己每一天都在进步. import java.text.DateFormat; import java.text.ParseException; import ja ...
- 使用ddns搭建免费服务器
[使用ddns搭建免费服务器] 第一步 tplink路由器提供了ddns服务,它为用户免费提供一个子tpddns.cn下的子域名,映射到你的路由器上.当启用后,只在要能接入互联网的地方,都能过此域名, ...