二叉树遍历(前序、中序、后序、层次、深度优先、广度优先遍历)

描述

解析

递归方案

很简单,先左孩子,输出根,再右孩子。

非递归方案

因为访问左孩子后要访问右孩子,所以需要栈这样的数据结构。

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(二叉树的中序遍历) ☆☆☆的更多相关文章

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

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

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

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

  3. 【LeetCode】Binary Tree Inorder Traversal(二叉树的中序遍历)

    这道题是LeetCode里的第94道题. 题目要求: 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单 ...

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

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

  5. Leetcode94. Binary Tree Inorder Traversal二叉树的中序遍历(两种算法)

    给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 递归: class So ...

  6. [LeetCode] 144. Binary Tree Preorder Traversal 二叉树的先序遍历

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

  7. Leetcode 94 Binary Tree Inorder Traversal 二叉树

    二叉树的中序遍历,即左子树,根, 右子树 /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *lef ...

  8. [LeetCode] 145. Binary Tree Postorder Traversal 二叉树的后序遍历

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

  9. [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. 四: scrapy爬虫框架

    5.爬虫系列之scrapy框架   一 scrapy框架简介 1 介绍 (1) 什么是Scrapy? Scrapy是一个为了爬取网站数据,提取结构性数据而编写的应用框架,非常出名,非常强悍.所谓的框架 ...

  2. sql 查询字段是中文/英文/数字 正则表达式

    一.包含中文字符 select * from 表名 where 列名 like '%[吖-座]%' 二.包含英文字符 select * from 表名 where 列名 like '%[a-z]%' ...

  3. 1.1.1 vue-cli脚手架工具

    参考文档: windows下npm安装vue(以下教程大部分都是参考这篇博客的,按照着这篇博客自己实现了一遍) npm安装vue-cli脚手架 一.前言 npm:nodejs下的包管理器,安装好nod ...

  4. eclipse配置tomcat后出现:java virtual machine launcher Error:Could not ……,Program will exit.

    原贴在stack overflow上:https://stackoverflow.com/questions/50085750/apache-tomcat-9-x-not-working-with-e ...

  5. leecode第七十题(爬楼梯)

    class Solution { public: int climbStairs(int n) { vector<unsigned long long> num;//斐波那契数列 num. ...

  6. opencv4.0 cuda10 编译速度太慢

    opencv4.0 cuda10 对比不带cuda的时候,编译速度太慢,主要卡在cuda的编译上. 参考http://answers.opencv.org/question/5090/why-open ...

  7. Anaconda 简单介绍 -- 环境管理

    前面介绍了 Anaconda 的安装,接下来介绍一下 简单使用,后续并实时更新. 常用操作命令: 环境操作 1.查看环境管理的全部命令帮助: conda env -h 2.查看当前系统下的环境: co ...

  8. python中文件的读和写操作

    一.打开文件 data = open("yesterday",encoding="utf-8").read() # python默认的打字符编码是unicode ...

  9. redis在php运行时出现错误

    我的redis版本:3.2.8. redis安装教程,参考官方网站: https://redis.io/download 在网上多番查找,很多说是配置文件redis.conf中的: # bind 12 ...

  10. 算法:最短路径之弗洛伊德(Floyd)算法

    https://cloud.tencent.com/developer/article/1012420 为了能讲明白弗洛伊德(Floyd)算法的主要思想,我们先来看最简单的案例.图7-7-12的左图是 ...