[LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆
二叉树遍历(前序、中序、后序、层次、深度优先、广度优先遍历)
描述

解析
递归方案
很简单,先左孩子,输出根,再右孩子。
非递归方案
因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构。
1.指针指向根,根入栈,指针指向左孩子。把左孩子当作子树的根,继续前面的操作。
2.如果某个节点的左孩子不存在,节点出栈,指针指向节点的右孩子。把这个右节点当作根, 继续前面的操作。
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
List<Integer> list = new ArrayList<>();
public List<Integer> inorderTraversal(TreeNode root) {
if (null == root) {
return list;
}
inorderTraversal(root.left);
list.add(root.val);
inorderTraversal(root.right);
return list;
}
}
/**
* 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> list = new ArrayList<>();
if (null == root) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode curNode = root;
while (null != curNode || !stack.isEmpty()) {
if (null != curNode) {
stack.push(curNode);
curNode = curNode.left;
} else {
curNode = stack.pop();
list.add(curNode.val);
curNode = curNode.right;
}
}
return list;
}
}
/**
* 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> list = new ArrayList<>();
if (null == root) {
return list;
}
Stack<TreeNode> stack = new Stack<>();
TreeNode curNode = root;
while (curNode != null || !stack.isEmpty()) {
while (curNode != null) { // Travel to each node's left child, till reach the left leaf
stack.push(curNode);
curNode = curNode.left;
}
curNode = stack.pop(); // Backtrack to higher level node A
list.add(curNode.val); // Add the node to the result list
curNode = curNode.right; // Switch to A'right branch
}
return list;
}
}
[LeetCode] 94. Binary Tree Inorder Traversal(二叉树的中序遍历) ☆☆☆的更多相关文章
- [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】Binary Tree Inorder Traversal(二叉树的中序遍历)
这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...
- [LeetCode] Binary Tree Inorder Traversal 二叉树的中序遍历
Given a binary tree, return the inorder traversal of its nodes' values. For example:Given binary tre ...
- Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)
给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class So ...
- [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- Leetcode 94 Binary Tree Inorder Traversal 二叉树
二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...
- [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历
Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary ...
- [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. mysql 语句
基础语句 创建表 DROP TABLE IF EXISTS student;CREATE TABLE student ( id ) NOT NULL AUTO_INCREMENT, sname ) N ...
- 突变注释工具SnpEff,Annovar,VEP,oncotator比较分析--转载
https://www.jianshu.com/p/6284f57664b9 目前对于variant进行注释的软件主要有4个: Annovar, SnpEff, VEP(variant Effect ...
- 【Cucumber】【问题集锦】
[问题一]invalid byte sequence in GBK"问题 invalid byte sequence in UTF-8"问题 参考地址:http://fantaxy ...
- Java 通过get post 请求url
1️⃣.已获取小程序的access_token 为例,通过Get请求url import com.alibaba.fastjson.JSONObject; String wechatUrl = &qu ...
- OpenModelica读取文件
parameter String file = Modelica.Utilities.Files.loadResource("J:/git/tcs/tcs.txt"); 将文件名变 ...
- Java 8里面lambda的最佳实践
Java 8已经推出一段时间了,越来越多开发人员选择升级JDK,这条热门动弹里面看出,JDK7最多,其次是6和8,这是好事! 在8 里面Lambda是最火的主题,不仅仅是因为语法的改变,更重要的是带来 ...
- Django - Python3 配置 MySQL
在使用 PyMySQL 之前,我们需要确保 PyMySQL 已安装 具体安装使用方法,可参考 Python3 - MySQL适配器 PyMySQL Django 如何链接 MySQL 数据库, 需要在 ...
- google搜索小技巧
google搜索小技巧 一.总结 一句话总结:But most people may not be using Google search to its full potential.Want to ...
- Redis入门指南之二(安装及配置)
本节主要内容 1. 前言2. redis安装3. 启动和停止Redis 1. 前言 安装Redis需要知道自己需要哪个版本,有针对性的安装,比如如果需要redis GEO这个地理集合的特性,那么red ...
- 20165327 2017-2018-2 《Java程序设计》第7周学习总结
20165327 2017-2018-2 <Java程序设计>第7周学习总结 教材内容总结 第十一章 (一)MySQL数据库服务器 下载安装MySQL服务器 启动MySQL数据库服务器 在 ...