094 Binary Tree Inorder Traversal 中序遍历二叉树
给定一个二叉树,返回其中序遍历。
例如:
给定二叉树 [1,null,2,3],
1
\
2
/
3
返回 [1,3,2].
说明: 递归算法很简单,你可以通过迭代算法完成吗?
详见:https://leetcode.com/problems/binary-tree-inorder-traversal/description/
Java实现:
递归实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
if(root==null){
return res;
}
helper(root,res);
return res;
}
private void helper(TreeNode root,List<Integer> res){
if(root==null){
return;
}
helper(root.left,res);
res.add(root.val);
helper(root.right,res);
}
}
非递归实现:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res=new ArrayList<Integer>();
if(root==null){
return res;
}
Stack<TreeNode> stk=new Stack<TreeNode>();
while(root!=null|!stk.isEmpty()){
if(root!=null){
stk.push(root);
root=root.left;
}else{
root=stk.pop();
res.add(root.val);
root=root.right;
}
}
return res;
}
}
094 Binary Tree Inorder Traversal 中序遍历二叉树的更多相关文章
- Leetcode 94. Binary Tree Inorder Traversal (中序遍历二叉树)
Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tr ...
- [LeetCode] Binary Tree Inorder Traversal 中序排序
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- [LeetCode]题解(python):094 Binary Tree Inorder Traversal
题目来源 https://leetcode.com/problems/binary-tree-inorder-traversal/ iven a binary tree, return the ino ...
- Java for LeetCode 094 Binary Tree Inorder Traversal
解题思路: 中序遍历,左子树-根节点-右子树 JAVA实现如下: public List<Integer> inorderTraversal(TreeNode root) { List&l ...
- LeetCode OJ:Binary Tree Preorder Traversal(前序遍历二叉树)
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- LeetCode 094 Binary Tree Inorder Traversal
方法一:(递归) class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int ...
- 刷题94. Binary Tree Inorder Traversal
一.题目说明 题目94. Binary Tree Inorder Traversal,给一个二叉树,返回中序遍历序列.题目难度是Medium! 二.我的解答 用递归遍历,学过数据结构的应该都可以实现. ...
- [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 ...
随机推荐
- javase练习题--每天写写
package com.javaTestDemo; import java.util.Scanner; public class JavaTest1 { public static void main ...
- ORA-03113: end-of-file on communication channel (通信通道的文件结尾)
今天有现场反应:数据库连不上了,提示什么归档日志有问题:又问了现场有做过什么特别操作,答曰没有,出问题后,只是重启了操作系统. 现场环境oracle11.0.2.3. 于是远程查看数据库状态,发现数据 ...
- Linux下压缩某个文件夹命令
tar -zcvf /home/xahot.tar.gz /xahot tar -zcvf 打包后生成的文件名全路径 要打包的目录 例子:把/xahot文件夹打包后生成一个/home/xahot.ta ...
- (QACNN)自然语言处理:智能问答 IBM 保险QA QACNN 实现笔记
follow: https://github.com/white127/insuranceQA-cnn-lstm http://www.52nlp.cn/qa%E9%97%AE%E7%AD%94%E7 ...
- JavaScript实现按键记录,并在关掉网页之前把记录的内容post出去
最近陈老师让我给新架构加一个按键记录的业务.去学习了JavaScript,网上找了一些代码,最后写出来了: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTM ...
- webpack安装后package-lock.json 的作用
这个文件主要功能是确定当前安装的包的依赖,以便后续重新安装的时候生成相同的依赖,而忽略项目开发过程中有些依赖已经发生的更新. 避免了依赖升级和当前项目不兼容!
- HDU2896(AC自动机入门题)
病毒侵袭 Time Limit:1000MS Memory Limit:32768KB Description 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这 ...
- vue watch 深度监听以及立即监听
vue watch对象可以监听数据,数据发生变化,处理函数 watch虽可以监听,但只是浅监听,只监听数据第一层或者第二层.比如对于整个对象的监听,需要用到深度监听 vm.$watch('obj',f ...
- CPU、内存、硬盘分区的检测.py
cpu_mem_directories.py CPU.内存.硬盘分区的检测 #!/usr/bin/env python #coding:utf-8 import psutil import tim ...
- Android开发--Activity
一:Activity生命周期 (1)Activity生命周期中的几种方法: protected void onCreate(Bundle savedInstanceState): protected ...