题目链接

题目大意:中序遍历二叉树。先序见144,后序见145。

法一:DFS,没啥说的,就是模板DFS。代码如下(耗时1ms):

     public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
dfs(res, root);
return res;
}
private void dfs(List<Integer> res, TreeNode root) {
if(root != null) {
dfs(res, root.left);
res.add(root.val);
dfs(res, root.right);
}
}

法二:非递归。与先序类似。代码如下(耗时2ms):

     public List<Integer> inorderTraversal(TreeNode root) {
Stack<TreeNode> s = new Stack<TreeNode>();
List<Integer> res = new ArrayList<Integer>();
TreeNode tmp = root;
while(!s.isEmpty() || tmp != null) {
//压入左孩子结点
while(tmp != null) {
s.push(tmp);
tmp = tmp.left;
}
//如果栈非空,弹出顶结点,遍历右子树
if(!s.isEmpty()) {
tmp = s.pop();
res.add(tmp.val);
tmp = tmp.right;
}
}
return res;
}

94.Binary Tree Inorder Traversal---二叉树中序非递归遍历的更多相关文章

  1. [leetcode]94. Binary Tree Inorder Traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] ...

  2. 94 Binary Tree Inorder Traversal(二叉树中序遍历Medium)

    题目意思:二叉树中序遍历,结果存在vector<int>中 解题思路:迭代 迭代实现: /** * Definition for a binary tree node. * struct ...

  3. [Leetcode] Binary tree inorder traversal二叉树中序遍历

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

  4. [leetcode] 94. Binary Tree Inorder Traversal 二叉树的中序遍历

    题目大意 https://leetcode.com/problems/binary-tree-inorder-traversal/description/ 94. Binary Tree Inorde ...

  5. LeetCode:94_Binary Tree Inorder Traversal | 二叉树中序遍历 | Medium

    题目:Binary Tree Inorder Traversal 二叉树的中序遍历,和前序.中序一样的处理方式,代码见下: struct TreeNode { int val; TreeNode* l ...

  6. 145.Binary Tree Postorder Traversal---二叉树后序非递归遍历

    题目链接 题目大意:后序遍历二叉树. 法一:普通递归,只是这里需要传入一个list来存储遍历结果.代码如下(耗时1ms): public List<Integer> postorderTr ...

  7. LeetCode 94. Binary Tree Inorder Traversal 二叉树的中序遍历 C++

    Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [,,] \ / Out ...

  8. [LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆

    二叉树遍历(前序.中序.后序.层次.深度优先.广度优先遍历) 描述 解析 递归方案 很简单,先左孩子,输出根,再右孩子. 非递归方案 因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构. 1.指针 ...

  9. LeetCode OJ:Binary Tree Inorder Traversal(中序遍历二叉树)

    Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...

随机推荐

  1. [九]SpringBoot 之 定时任务

    代码: package me.shijunjie.config; import org.springframework.context.annotation.Configuration; import ...

  2. Contest 4

    A:cf原题.当然是不是也没什么关系. #include<iostream> #include<cstdio> #include<cstdlib> #include ...

  3. WordPress忘记密码找回登录密码的四种行之有效的方法

    WordPress忘记密码找回登录密码的四种行之有效的方法 PS:20170214更新,感谢SuperDoge同学提供的方法,登入phpMyAdmin后,先从左边选自己的数据库,然后点上面的 SQL ...

  4. oracle获取clob调优

    引用Oracle.DataAccess.dll,,在oracle 安装目录下的D:\oracle\product\10.2.0\db_1\ODP.NET\bin\1.x\Oracle.DataAcce ...

  5. MySQL中文全文检索demoSQL

    一.概述      MySQL全文检索是利用查询关键字和查询列内容之间的相关度进行检索,可以利用全文索引来提高匹配的速度. 二.语法      MATCH (col1,col2,...) AGAINS ...

  6. 驱动之LCD的介绍与应用20170209

    本文主要介绍的是LCD的介绍与应用,直接看个人笔记即可:

  7. 关于NUL

    问题:正常的order by不起作用了,如下图 分析:使用notepad++打开,发现 NUL以字符'\0'作为字符串结束标志.'\0'是一个ASCII码为0的字符,从ASCII码表中可以看到ASCI ...

  8. Tensorboard教程:显示计算图中节点信息

    Tensorboard显示计算图节点信息 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 强烈推荐Tensorflow实战Google深度学习框架 实验平台: Tensorflow1 ...

  9. Intellj IDEA使用技巧记录

    ▲.Intellj IDEA光标变成了insert光标状态 且不能编辑操作: https://blog.csdn.net/aosica321/article/details/52787418 ▲.在i ...

  10. oracle的小语句

    select * from v$nls_parameters; 查询数据库中现在的常量 alter session set NLS_DATE_FORMAT='yyyy-mm-dd'; 更改日期显示方式