[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
随机推荐
- Could not get lock /var/lib/dpkg/lock - open (11: Resource temporarily unavailable)
To fix it, just kill the apt process. ps -aux | grep -i apt sudo kill -9 "the process ID"
- spingMVC+mybatis+spring-session共享内存配置
1. redis依赖: <dependency> <groupId>org.springframework.session</groupId> <artifa ...
- IIS 添加 MIME
参考:https://blog.brain1981.com/727.html 在项目的 Web.Config 里下添加如下段落即可: <?xml version="1.0" ...
- git之sourceTree使用github和码云的代码小结
16.使用git出现的错误记录 15. Permission denied (publickey)错误: git远程库与本地库同步 git设置ssh公钥 Bad escape character ' ...
- CentOS7 安装kafka集群
1. 环境准备 JDK1.8 ZooKeeper集群(参见本人博文) Scala2.12(如果需要做scala开发的话,安装方法参见本人博文) 本次安装的kafka和zookeeper集群在同一套物理 ...
- 批量IP自动ping脚本
批量IP自动ping脚本ping.sh 在同一目录新建一个名为pingip的文件,并以每行一个IP的方式罗列.使用sh命令执行ping.sh #!/bin/bash IP_LIST=`cat ping ...
- ElasicSearch(4) 与jest结合
https://spring.io/projects/spring-data-elasticsearch https://docs.spring.io/spring-data/elasticsearc ...
- 阿里云服务器 CentOS 安装Mysql 5.6
下载:https://dev.mysql.com/downloads/file/?id=471181 第一步: 安装mysql5姿势是要先安装带有可用的mysql5系列社区版资源的rpm包 [ro ...
- 将LinkedHashMap转换为需要的对象
项目中,在获取json数据转换为list类型以后,本来以为可以直接使用,结果在使用中报错“java.lang.ClassCastException: java.util.LinkedHashMap c ...
- board_led.h/board_led.c
/******************************************************************************* Filename: board_led ...