LeetCode94. 二叉树的中序遍历(java,python,递归,迭代)

2023-10-15 14:18

本文主要是介绍LeetCode94. 二叉树的中序遍历(java,python,递归,迭代),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1. 题目

给定一个二叉树,返回它的中序 遍历。

示例:

输入: [1,null,2,3]
   1\2/3

输出: [1,3,2]

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

2. 解答

方法一:迭代

Java 实现:

/*** Definition for a binary tree node.* public class TreeNode {*     int val;*     TreeNode left;*     TreeNode right;*     TreeNode(int x) { val = x; }* }*/
/*(left, in, right)*/
class Solution {public List<Integer> inorderTraversal(TreeNode root) {ArrayList<Integer> res = new ArrayList<>();if (root == null)return res;/*链表*/LinkedList<TreeNode> stack = new LinkedList();TreeNode node = root;while(node != null || !stack.isEmpty()){while(node != null){stack.push(node);node = node.left;}node = stack.pop();res.add(node.val);node = node.right;}return res;}
}

python实现迭代:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def inorderTraversal(self, root: TreeNode) -> List[int]:# 迭代stack = []res = []while root or stack:while root:stack.append(root)root = root.leftroot = stack.pop()res.append(root.val)root = root.rightreturn res

方法二:递归
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<>();if(root==null){return res;}inorder(root, res);return res;}public void inorder(TreeNode root, List<Integer> res) {if(root.left!=null){inorder(root.left, res);}res.add(root.val);if(root.right!=null){inorder(root.right, res);}}
}

python实现递归:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = Noneclass Solution:def inorderTraversal(self, root: TreeNode) -> List[int]:# 递归res = []self.inOrder(root,res)return resdef inOrder(self, root, res):if root:self.inOrder(root.left,res)res.append(root.val)self.inOrder(root.right,res)

这篇关于LeetCode94. 二叉树的中序遍历(java,python,递归,迭代)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/218258

相关文章

Spring Boot 2.7.8 集成 Thymeleaf的最佳实践与常见问题讨论

《SpringBoot2.7.8集成Thymeleaf的最佳实践与常见问题讨论》本文详细介绍了如何将SpringBoot2.7.8与Thymeleaf集成,从项目依赖到配置文件设置,再到控制器... 目录前言一、如何构建SpringBoot应用1、项目依赖 (pom.XML)2、控制器类3、Thymelea

SpringBoot项目jar依赖问题报错解析

《SpringBoot项目jar依赖问题报错解析》本文主要介绍了SpringBoot项目中常见的依赖错误类型、报错内容及解决方法,依赖冲突包括类找不到、方法找不到、类型转换异常等,本文给大家介绍的非常... 目录常见依赖错误类型及报错内容1. 依赖冲突类错误(1) ClassNotFoundExceptio

Python使用Spire.PDF实现为PDF添加水印

《Python使用Spire.PDF实现为PDF添加水印》在现代数字化办公环境中,PDF已成为一种广泛使用的文件格式,尤其是在需要保持文档格式时,下面我们就来看看如何使用Python为PDF文件添加水... 目录一、准备工作二、实现步骤1. 导入必要的库2. 创建 PdfDocument 对象3. 设置水印

python循环引用和解决过程

《python循环引用和解决过程》文章讨论了在Python中解决循环引用问题的几种方法,包括延迟导入、使用`importlib`、重构代码和类型提示的前向引用,这些方法可以帮助开发者避免导入循环导致的... 目录模拟循环引用解决python循环引用的方法有几种,以下是一些常见的解决android方案常见问题

python在word中插入目录和更新目录实现方式

《python在word中插入目录和更新目录实现方式》文章主要介绍了如何在Word文档中插入和更新目录,并提供了具体的代码示例,插入目录时,需要使用`TablesOfContents`对象,并设置使用... 目录1、插入目录2、更新目录总结1、插入目录需要用到对象:TablesOfContents目录的

springboot控制bean的创建顺序

《springboot控制bean的创建顺序》本文主要介绍了spring-boot控制bean的创建顺序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录1、order注解(不一定有效)2、dependsOn注解(有效)3、提前将bean注册为Bea

Java中的ConcurrentBitSet使用小结

《Java中的ConcurrentBitSet使用小结》本文主要介绍了Java中的ConcurrentBitSet使用小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、核心澄清:Java标准库无内置ConcurrentBitSet二、推荐方案:Eclipse

java中的Supplier接口解析

《java中的Supplier接口解析》Java8引入的Supplier接口是一个无参数函数式接口,通过get()方法延迟计算结果,它适用于按需生成场景,下面就来介绍一下如何使用,感兴趣的可以了解一下... 目录1. 接口定义与核心方法2. 典型使用场景场景1:延迟初始化(Lazy Initializati

Java中ScopeValue的使用小结

《Java中ScopeValue的使用小结》Java21引入的ScopedValue是一种作用域内共享不可变数据的预览API,本文就来详细介绍一下Java中ScopeValue的使用小结,感兴趣的可以... 目录一、Java ScopedValue(作用域值)详解1. 定义与背景2. 核心特性3. 使用方法

spring中Interceptor的使用小结

《spring中Interceptor的使用小结》SpringInterceptor是SpringMVC提供的一种机制,用于在请求处理的不同阶段插入自定义逻辑,通过实现HandlerIntercept... 目录一、Interceptor 的核心概念二、Interceptor 的创建与配置三、拦截器的执行顺