[leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历
Given a binary tree, return the inorder traversal of its nodes' values.
Example:
Input: [1,null,2,3]
1
\
2
/
3 Output: [1,3,2]
Follow up: Recursive solution is trivial, could you do it iteratively?
题意:
二叉树中序遍历
Solution1: Recursion
code
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<>();
storeInorder(root, list);
return list;
}
public void storeInorder(TreeNode node, List<Integer> list) {
if(node == null) return;
storeInorder(node.left, list);
list.add(node.val);
storeInorder(node.right, list);
}
}
Solution2: Iteration
code
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
//left -- root -- right
ArrayList<Integer> result = new ArrayList<>();
Stack<TreeNode> s = new Stack<>();
TreeNode p = root;
while(!s.isEmpty() || p!=null){
if(p!=null){
s.push(p);
p = p.left;
}else{
p =s.pop();
result.add(p.val);
p = p.right;
}
}
return result;
}
}
[leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历的更多相关文章
- 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)
题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...
- 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] 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 OJ:Binary Tree Inorder Traversal(中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode 94 Binary Tree Inorder Traversal 二叉树
二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...
- [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆
二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...
- 49. leetcode 94. Binary Tree Inorder Traversal
94. Binary Tree Inorder Traversal 二叉树的中序遍历 递归方法: 非递归:要借助栈,可以利用C++的stack
随机推荐
- DevExpress.XtraGrid.GridControl中数据源的绑定问题
在利用DevExpress.XtraGrid.GridControl作为一个可编辑的表格控件时,在利用控件之前,先将一个初始化的DataTable对象作为GridControl的数据源进行绑定.可是在 ...
- 3、PHP中常用的数据库操作函数解析
mysql_connect 连接数据库 mysql_select_db 选择需要操作的数据库 mysql_query 执行数据库操作语句 mysql_fetch_array 以数组的形式返回每行查询 ...
- c++11模拟boost元占位符placeholder
准备实现meta programming的fold函数,发现自己缺少占位符实现,这样传入fold的transform op类(元函数)都不得不另外写个外覆类,其实我觉得没啥不好,简单直接,说实话干扰什 ...
- ThreadLocal的学习
一 用法ThreadLocal用于保存某个线程共享变量:对于同一个static ThreadLocal,不同线程只能从中get,set,remove自己的变量,而不会影响其他线程的变量.1.Threa ...
- shell脚本大小写转换
几个方法 1.tr命令 2.sed替换 3.awk的tolower() toupper() 4.perl语言 详见 http://blog.51cto.com/wangxiaoyu/197623 L ...
- 微信小程序如何设置服务器配置
最近微信小程序在it界火了起来,公司也要求我们开始接触微信小程序,废话不多说直接从配置微信小程序开始 1,首先,登录 https://mp.weixin.qq.com,(这里默认你已经获取到微信小程序 ...
- [UE4]Widget Switcher:控件切换器
一.Widget Switcher可以有很多子控件,但一次只会显示一个子控件.所有的子控件默认情况下都是充满整个Widget Switcher容器 二.Widget Switcher.Active W ...
- LTE学习笔记(一)——背景知识
一.标准化组织 无线通信技术的演进离不开一些标准化组织. 1.ITU(International Telecommunication Union) 国际电信联盟,主要任务是制定标准,分配无线频谱资源, ...
- 使用Mechanize实现自动化表单处理
使用Mechanize实现自动化表单处理 mechanize是对urllib2的部分功能的替换,能够更好的模拟浏览器行为,在web访问控制方面做得更全面 mechanize的特点: 1 http, ...
- Oracle 唯一 约束(unique constraint) 与 索引(index) 关系说明
一. 官网对Unique Constraints说明 http://download.oracle.com/docs/cd/E11882_01/server.112/e16508/datainte.h ...