94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中
解题思路:迭代
迭代实现:
/**
* 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> ans;
if(root){
stack<TreeNode*> s;
TreeNode* temp=root;
while(temp!=NULL||!s.empty()){
while(temp!=NULL){
s.push(temp); //将所有的左节点入栈
temp=temp->left;
}
temp=s.top();
s.pop();
ans.push_back(temp->val); //添加最下面的左节点
temp=temp->right; //添加倒数第二个左节点的右节点
}
}
return ans;
}
};
94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)的更多相关文章
- [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...
- LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium
题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...
- [Leetcode] Binary tree inorder traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- 144 Binary Tree Preorder Traversal(二叉树先序遍历Medium)
题目意思:二叉树先序遍历,结果存在vector<int>中 解题思路:1.递归(题目中说用递归做没什么意义,我也就贴贴代码吧) 2.迭代 迭代实现: class Solution { pu ...
- [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 二叉树的中序遍历 C++
Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...
- Leetcode 94 Binary Tree Inorder Traversal 二叉树
二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...
- 94.Binary Tree Inorder Traversal---二叉树中序非递归遍历
题目链接 题目大意:中序遍历二叉树.先序见144,后序见145. 法一:DFS,没啥说的,就是模板DFS.代码如下(耗时1ms): public List<Integer> inorder ...
随机推荐
- C#调用webservers实现天气预报
一:截图 二:实现步骤 1.引入Web服务.在VS中项目上右击→添加服务引用. 2.在弹出的添加服务引用窗口,录入web服务地址和引用后的命名空间. 三:源代码 using System; using ...
- zoj 3462
#include <cstdio> #include <cmath> #include <algorithm> #include <iostream> ...
- Apache-Tika解析XML文档
通常在使用爬虫时,爬取到网上的文章都是各式各样的格式处理起来比较麻烦,这里我们使用Apache-Tika来处理XML格式的文章,如下: package com.mengyao.tika.app; im ...
- 杠杠做的全屏随鼠标滚动显示图片,类似于PPT效果
图片有22张,是一张张加载的,耐心点,鼠标一直尝试向下滚就行了. 图片来自<天空之境:乌尤尼盐沼>,一个好美好美的地方 引个流,欢迎去我的博客看看:http://blog.cxycs.co ...
- phoneGap 中修改生成APP的名字
最近忙着研究移动开发的事情,去学习了一下移动开发的东西,例如eclipse和phoneGap进行配合使用,感觉还是不错的,先针对eclipse和phoneGa的平台搭建这里先不在详细说啦,主要还是我们 ...
- 将JDBC ResultSet结果集变成List
private List<Map<String, Object>> list = new ArrayList<Map<String,Object>>() ...
- Android-Socket传输 GPRS网络
手机使用GPRS网络与server进行Socket通信,代码下载地址:http://download.csdn.net/detail/wu20093346/7768481 用UDP协议与Socket调 ...
- CreateProcess函数具体解释
CreateProcess说明:WIN32API函数CreateProcess用来创建一个新的进程和它的主线程,这个新进程执行指定的可执行文件. 函数原型:BOOL CreateProcess( ...
- [转] 学习HTML/JavaScript/PHP 三者的关系以及各自的作用
1.What is HTML? When you write a normal document using a word processor like Microsoft Word/Office, ...
- TCP/IP协议原理与应用笔记08:对等层和对等实体
1. 我们直接通过下面这个图,就可以直观了解: Outlook :收发邮件的软件组件. IE:浏览器. CutFTP:文件传输工具. 小结:因为为了完成不同的功能,所以会出现不同实体,这些不同的实体为 ...