当前位置: 首页 > news >正文

【Hot100】LeetCode—94. 二叉树的中序遍历

目录

  • 1- 思路
    • 栈-统一遍历
  • 2- 实现
    • ⭐94. 二叉树的中序遍历——题解思路
  • 3- ACM实现


  • 原题连接:94. 二叉树的中序遍历

1- 思路

栈-统一遍历

二叉树的处理遍历过程,主要分为两步

  • 1- 访问节点
  • 2- 处理节点

中序遍历

  • 中序遍历的顺序是 ——> 左 中 右 ——> 给我们的又是根节点,需要从根节点开始访问
  • 例如二叉树,可以看到从根节点 5 开始访问,但不能先处理节点 5
    • 先访问 5 ——> 4 ——> 1 ,其中 1 才是我们需要处理的结点。

image.png
借助栈实现

  • 使用一个指针,来遍历二叉树,访问节点后将其入栈
  • 直到遍历到 null,比如指针到 1 的左节点此时是 null 了,就需要栈中弹出元素,处理第一个结点就是 1 ,之后将 1 加入结果集

image.png


2- 实现

⭐94. 二叉树的中序遍历——题解思路

在这里插入图片描述

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();Stack<TreeNode> st = new Stack<>();if(root==null){return res;}// 指针TreeNode cur = root;while(cur!=null || !st.isEmpty()){if(cur!=null){st.push(cur);cur = cur.left;}else{cur = st.pop();res.add(cur.val);cur = cur.right;}}return res;}
}

3- ACM实现

public class inorderT {public static class TreeNode {int val;TreeNode left;TreeNode right;TreeNode() {}TreeNode(int val) { this.val = val; }TreeNode(int val, TreeNode left, TreeNode right) {this.val = val;this.left = left;this.right = right;}}public static TreeNode build(String str){if(str == null || str.length()==0){return null;}String input = str.replace("[","");input = input.replace("]","");String[] parts = input.split(",");Integer[] nums = new Integer[parts.length];for(int i  = 0 ; i < parts.length;i++){if(!parts[i].equals("null")){nums[i] = Integer.parseInt(parts[i]);}else{nums[i] = null;}}Queue<TreeNode> queue = new LinkedList<>();TreeNode root = new TreeNode(nums[0]);queue.offer(root);int index = 1;while(!queue.isEmpty()&& index<parts.length){TreeNode node = queue.poll();if(index<nums.length && nums[index]!=null){node.left = new TreeNode(nums[index]);queue.offer(node.left);}index++;if(index<nums.length && nums[index]!=null){node.right = new TreeNode(nums[index]);queue.offer(node.right);}index++;}return root;}public static List<Integer> inorderT(TreeNode root){// 借助 栈 + 指针TreeNode cur = root;Stack<TreeNode> st = new Stack<>();List<Integer> res = new ArrayList<>();if(root==null){return res;}// 遍历while (cur!=null || !st.isEmpty()){// 一直遍历if(cur!=null){st.push(cur);cur = cur.left;}else{// 处理节点cur = st.pop();res.add(cur.val);cur = cur.right;}}return res;}public static void main(String[] args) {Scanner sc = new Scanner(System.in);String input = sc.nextLine();TreeNode root = build(input);List<Integer> res = inorderT(root);System.out.println(res.toString());}
}

http://www.mrgr.cn/news/6191.html

相关文章:

  • 什么是Redis大key问题?如何解决?
  • mpv播放器在rk3399上配置硬解码
  • 【Windows】深度学习环境部署
  • 【Linux操作系统】进程间通信(1)
  • 深入理解TCP选择性确认(SACK):优化网络传输的机制
  • 6款大学生电脑里的必装软件,装进电脑慢慢用
  • SQLite 插入一行并返回主键
  • 代码随想录算法训练营day52:图03:101. 孤岛的总面积;102. 沉没孤岛;103. 水流问题
  • 【qt】多线程
  • 零基础学习Redis(3) -- Redis常用命令
  • 【达梦数据库】锁超时的处理方法-错误码[-6407]
  • EmguCV学习笔记 VB.Net 第5章 图像变换
  • 通过Qt Creator Plugin开发Qt Creator插件-【金丹篇】
  • 设计模式 -- 概述
  • FPGA开发——DS18B20的使用(理论)
  • Mybatis(面试篇)
  • 软件测试最全面试题及答案整理(2024最新版)
  • 深入了解SOCKS v5协议:代理通信的安全通道
  • 协作新选择:即时白板在线白板软件分享
  • 工业4G路由器