leetcode 94 中序遍历模板
/**递归的写法
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
void MiddleTravel(TreeNode * root,vector<int>&result_vec) {
if(root!=NULL) {
MiddleTravel(root->left,result_vec);
result_vec.push_back(root->val);
MiddleTravel(root->right,result_vec);
}
}
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int>result_vec;
MiddleTravel(root,result_vec);
return result_vec;
}
};
下面是一个不用递归的写法,用栈:
/**
* 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) {
vector<int>result_vec;
stack<TreeNode *>q_stack;
TreeNode *NowNode=root;
while(NowNode||!q_stack.empty()) {
if(NowNode!=NULL) {
q_stack.push(NowNode);
NowNode=NowNode->left;
}
else {
NowNode=q_stack.top();
result_vec.push_back(NowNode->val);
q_stack.pop();
NowNode=NowNode->right;
}
}
return result_vec;
}
};
leetcode 94 中序遍历模板的更多相关文章
- [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历
题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...
- LeetCode 94. 二叉树的中序遍历(Binary Tree Inorder Traversal)
94. 二叉树的中序遍历 94. Binary Tree Inorder Traversal 题目描述 给定一个二叉树,返回它的 中序 遍历. LeetCode94. Binary Tree Inor ...
- LeetCode 94 | 基础题,如何不用递归中序遍历二叉树?
今天是LeetCode专题第60篇文章,我们一起来看的是LeetCode的94题,二叉树的中序遍历. 这道题的官方难度是Medium,点赞3304,反对只有140,通过率有63.2%,在Medium的 ...
- Java实现 LeetCode 94 二叉树的中序遍历
94. 二叉树的中序遍历 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? / ...
- 【LeetCode】94. 二叉树的中序遍历
94. 二叉树的中序遍历 知识点:二叉树:递归:Morris遍历 题目描述 给定一个二叉树的根节点 root ,返回它的 中序 遍历. 示例 输入:root = [1,null,2,3] 输出:[1, ...
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- LeetCode(94):二叉树的中序遍历
Medium! 题目描述: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗 ...
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
随机推荐
- Spring 配置定时器(注解+xml)方式—整理
一.注解方式 1. 在Spring的配置文件ApplicationContext.xml,首先添加命名空间 xmlns:task="http://www.springframework.or ...
- Install your Application into RaspberryPi2 and automatically start up
如何安装和卸载应用程序到RaspberryPi2? 如何配置应用程序在RaspberryPi2开机后自动启动? How to install your app into RaspberryPi2? H ...
- Linux下文件以及文件名编码转换
1.查看文件编码方式--file 文件名(但不是很准确) yang@mint-linux ~ $ file baidu.html baidu.html: HTML document, UTF-8 Un ...
- DRBD+NFS+Keepalived高可用环境
1.前提条件 准备两台配置相同的服务器 2.安装DRBD [root@server139 ~]# yum -y update kernel kernel-devel [root@server139 ~ ...
- 换个语言学一下 Golang (5)——运算符
运算符用于在程序运行时执行数学或逻辑运算. Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 接下来让我们来详细看看各个运算符的介绍. 算术运算符 下表 ...
- linux_1
注: 创建软连接: "ln -s xxx 路径1" 在路径1创建xxx的软连接 特点: 1.文件类型 l 2.相当于windows的快捷方式 创建硬链接: "ln xxx ...
- python_110_反射
class Dog(object): def __init__(self,name): self.name=name def eat(self): print('%s is eating '%self ...
- 字符串数组 输入3个字符串,要求按由小到大的字母顺序输出; 输入n个学生的姓名和学号到字符串数组中,在输入一个姓名,如果班级有该生则返回其信息,否则返回本班无此人
输入3个字符串,要求按由小到大的字母顺序输出 如 输入franch england china,输出结果是china england franch 三个数排序输出,比较三个数的大小怎么做? a=18 ...
- jquery html5 实现placeholder 兼容password ie6
<style type="text/css"> /* 设置提示文字颜色 */ ::-webkit-input-placeholder { color: #838383; ...
- Wash!!(HDU_6000)
传送门:Wash! 题意:有n台洗衣机,m台烘干机,给出了每台机器处理意见衣服的时间,而且没见机器同时只能处理一件衣服.问如何选择机器才能使洗完衣服的时间最短. 思路:建两个优先队列,一个表示洗衣机, ...