用递归与迭代完成二叉树的三种遍历

目录

二叉树的前序遍历

题目

前序遍历题目链接

递归代码

1.利用方法返回值的代码

2.返回值为void的代码

非递归实现前序遍历(利用栈stack) 

1.利用方法返回值的代码

2.返回值为void的代码 

二叉树的中序遍历

题目

:给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。

中序遍历题目链接

递归代码

1.利用方法返回值的代码

2.返回值为void的代码

非递归实现中序遍历(利用栈stack) 

1.利用方法返回值的代码

2.返回值为void的代码 

二叉树的后序遍历

题目

后序遍历题目链接

递归代码

1.利用方法返回值的代码

2.返回值为void的代码

非递归实现中序遍历(利用栈stack) 

1.利用方法返回值的代码

2.返回值为void的代码

完结撒花✿✿ヽ(°▽°)ノ✿✿


遍历(Traversal)是指沿着某条搜索路线,依次对树中每个结点均做一次且仅做一次访问          前序遍历(Preorder Traversal 亦称先序遍历)——访问根结点--->根的左子树--->根的右子树。
中序遍历(Inorder Traversal)——根的左子树--->根节点--->根的右子树。
后序遍历(Postorder Traversal)——根的左子树--->根的右子树--->根节点

二叉树的前序遍历

题目

:给你二叉树的根节点 root ,返回它节点值的 前序 遍历   

前序遍历题目链接

: https://leetcode.cn/problems/binary-tree-preorder-traversal/    

递归代码

1.利用方法返回值的代码

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> ret = new ArrayList<Integer>();if(root==null){return ret;}ret.add(root.val);List<Integer> leftTree=preorderTraversal(root.left);ret.addAll(leftTree);List<Integer> rightTree=preorderTraversal(root.right);ret.addAll(rightTree);return ret;}
}

2.返回值为void的代码

    // 前序遍历  根  左子树  右子树public void preOrder(BTNode root) {if (root == null) {return;}System.out.print(root.value + " ");preOrder(root.left);preOrder(root.right);}

非递归实现前序遍历(利用栈stack) 

1.利用方法返回值的代码

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> ret = new ArrayList<>();if (root == null) {return ret;}TreeNode cur = root;Deque<TreeNode> stack = new ArrayDeque<>();while (cur != null || !stack.isEmpty()) {while (cur != null) {stack.push(cur);//System.out.print(cur.val + " ");ret.add(cur.val);cur = cur.left;}TreeNode top = stack.pop();cur = top.right;}return ret;}
}

2.返回值为void的代码 

class Solution { public void preOrderNor(BTNode root) {if (root == null) {return;}BTNode cur = root;Deque<BTNode> stack = new ArrayDeque<>();while (cur != null || !stack.isEmpty()) {while (cur != null) {stack.push(cur);System.out.println(cur.value + " ");cur = cur.left;}BTNode top = stack.pop();cur = top.right;}}
}

二叉树的中序遍历

题目

:给定一个二叉树的根节点 root ,返回 它的 中序 遍历 。

中序遍历题目链接

:https://leetcode.cn/problems/binary-tree-inorder-traversal/ 

递归代码

1.利用方法返回值的代码

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> ret = new ArrayList<>();if (root == null) {return ret;}List<Integer> leftTree = inorderTraversal(root.left);ret.addAll(leftTree);ret.add(root.val);List<Integer> rightTree =inorderTraversal(root.right);ret.addAll(rightTree);return ret;}
}

2.返回值为void的代码

    // 中序遍历  左子树  根  右子树public void inOrder(BTNode root) {if (root == null) {return;}inOrder(root.left);System.out.print(root.value + " ");inOrder(root.right);}

非递归实现中序遍历(利用栈stack) 

1.利用方法返回值的代码

class Solution{public List<Integer> inorderTraversal(TreeNode root) {List<Integer> ret = new ArrayList<>();if (root == null) {return ret;}TreeNode cur = root;Deque<TreeNode> stack = new ArrayDeque<>();while (cur != null || !stack.isEmpty()) {while (cur != null) {stack.push(cur);cur = cur.left;}TreeNode top = stack.pop();ret.add(top.val);cur = top.right;}return ret;}
}

2.返回值为void的代码 

class Solution{    public void inOrderNor(BTNode root) {if (root == null) {return;}BTNode cur = root;Deque<BTNode> stack = new ArrayDeque<>();while (cur != null || !stack.isEmpty()) {while (cur != null) {stack.push(cur);cur = cur.left;}BTNode top = stack.pop();System.out.println(top.value + " ");cur = top.right;}}
}


二叉树的后序遍历

题目

:给你一棵二叉树的根节点 root ,返回其节点值的 后序遍历 

后序遍历题目链接

:https://leetcode.cn/problems/binary-tree-postorder-traversal/

递归代码

1.利用方法返回值的代码

class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> ret = new ArrayList<Integer>();if(root==null){return ret;}List<Integer> leftTree = postorderTraversal(root.left);ret.addAll(leftTree);List<Integer> rightTree = postorderTraversal(root.right);ret.addAll(rightTree);ret.add(root.val);return ret;}
}

2.返回值为void的代码

    // 后序遍历public void postOrder(BTNode root) {if (root == null) {return;}postOrder(root.left);postOrder(root.right);System.out.print(root.value + " ");}

非递归实现中序遍历(利用栈stack) 

1.利用方法返回值的代码

class Solution{public List<Integer> postorderTraversal(TreeNode root) {List<Integer> ret = new ArrayList<>();if (root == null) {return ret;}TreeNode cur = root;TreeNode prev = null;Deque<TreeNode> stack = new ArrayDeque<>();while (cur != null || !stack.isEmpty()) {while (cur != null) {stack.push(cur);cur = cur.left;}TreeNode top = stack.peek();if (top.right == null || top.right == prev) {//System.out.println(top.value + " ");ret.add(top.val);stack.pop();prev = top;} else {cur = top.right;}}return ret;}
}  

2.返回值为void的代码

class Solution{  public void postOrderNor(BTNode root) {if (root == null) {return;}BTNode cur = root;BTNode prev = null;Deque<BTNode> stack = new ArrayDeque<>();while (cur != null || !stack.isEmpty()) {while (cur != null) {stack.push(cur);cur = cur.left;}BTNode top = stack.peek();if (top.right == null || top.right == prev) {System.out.println(top.value + " ");stack.pop();prev = top;} else {cur = top.right;}}}
}

完结撒花✿✿ヽ(°▽°)ノ✿✿

                           

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/70610.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

windows pip安装出现 error: Microsoft Visual C++ 14.0 is required

可参考&#xff1a;如何解决 Microsoft Visual C 14.0 or greater is required. Get it with “Microsoft C Build Tools“_不吃香菜的小趴菜的博客-CSDN博客 一、安装Visual Studio2022 1、下载&#xff1a;下载 Visual Studio Tools - 免费安装 Windows、Mac、Linux 我这使…

Linux 终端命令之文件浏览(1) cat

Linux 文件浏览命令 cat, more, less, head, tail&#xff0c;此五个文件浏览类的命令皆为外部命令。 hannHannYang:~$ which cat /usr/bin/cat hannHannYang:~$ which more /usr/bin/more hannHannYang:~$ which less /usr/bin/less hannHannYang:~$ which head /usr/bin/he…

24近3年内蒙古大学自动化考研院校分析

今天给大家带来的是内蒙古大学控制考研分析 满满干货&#xff5e;还不快快点赞收藏 一、内蒙古大学 学校简介 内蒙古大学位于内蒙古自治区首府、历史文化名城呼和浩特市&#xff0c;距北京400余公里&#xff0c;是中华人民共和国成立后党和国家在民族地区创办的第一所综合大…

【【verilog典型电路设计之FIR滤波器的设计】】

verilog典型电路设计之FIR滤波器的设计 我们常用的FIR滤波器称为有限冲激响应 是一种常用的数字滤波器 &#xff0c;采用对已输入样值的加权和来形成它的输出。 对于输入序列X[n] 的FIR滤波器可用下图所示的结构示意图来表示&#xff0c;其中X[n] 是输入数据流。各级的输入连…

java八股文面试[java基础]——String StringBuilder StringBuffer

String类型定义&#xff1a; final String 不可以继承 final char [] 不可以修改 String不可变的好处&#xff1a; hash值只需要算一次&#xff0c;当String作为map的key时&#xff0c; 不需要考虑hash改变 天然的线程安全 知识来源&#xff1a; 【基础】String、StringB…

【云原生】Docker 详解(二):Docker 架构及工作原理

Docker 详解&#xff08;二&#xff09;&#xff1a;Docker 架构及工作原理 Docker 在运行时分为 Docker 引擎&#xff08;服务端守护进程&#xff09; 和 客户端工具&#xff0c;我们日常使用各种 docker 命令&#xff0c;其实就是在使用 客户端工具 与 Docker 引擎 进行交互。…

【Kafka】2.在SpringBoot中使用官方原生java版Kafka客户端

目 录 1. 新建一个消息生产者2. 新建一个消息消费者3. 测 试 在开始之前&#xff0c;需要先做点准备工作&#xff0c;用 IDEA 新建一个 Maven 项目&#xff0c;取名 kafka-study&#xff0c;然后删掉它的 src 目录&#xff0c;接着在 pom.xml 里面引入下面的依赖。这个项目的作…

[oneAPI] 手写数字识别-LSTM

[oneAPI] 手写数字识别-LSTM 手写数字识别参数与包加载数据模型训练过程结果 oneAPI 比赛&#xff1a;https://marketing.csdn.net/p/f3e44fbfe46c465f4d9d6c23e38e0517 Intel DevCloud for oneAPI&#xff1a;https://devcloud.intel.com/oneapi/get_started/aiAnalyticsToolk…

在本地搭建WAMP服务器并通过端口实现局域网访问(无需公网IP)

文章目录 前言1.Wamp服务器搭建1.1 Wamp下载和安装1.2 Wamp网页测试 2. Cpolar内网穿透的安装和注册2.1 本地网页发布2.2 Cpolar云端设置2.3 Cpolar本地设置 3. 公网访问测试4. 结语 前言 软件技术的发展日新月异&#xff0c;各种能方便我们生活、工作和娱乐的新软件层出不穷&a…

第57步 深度学习图像识别:CNN可视化(Pytorch)

基于WIN10的64位系统演示 一、写在前面 由于不少模型使用的是Pytorch&#xff0c;因此这一期补上基于Pytorch实现CNN可视化的教程和代码&#xff0c;以SqueezeNet模型为例。 二、CNN可视化实战 继续使用胸片的数据集&#xff1a;肺结核病人和健康人的胸片的识别。其中&…

考研算法第46天: 字符串转换整数 【字符串,模拟】

题目前置知识 c中的string判空 string Count; Count.empty(); //正确 Count ! null; //错误c中最大最小宏 #include <limits.h>INT_MAX INT_MIN 字符串使用发运算将字符加到字符串末尾 string Count; string str "liuda"; Count str[i]; 题目概况 AC代码…

【数据分析入门】Numpy进阶

目录 一、数据重塑1.1 透视1.2 透视表1.3 堆栈/反堆栈1.3 融合 二、迭代三、高级索引3.1 基础选择3.2 通过isin选择3.3 通过Where选择3.4 通过Query选择3.5 设置/取消索引3.6 重置索引3.6.1 前向填充3.6.2 后向填充 3.7 多重索引 四、重复数据五、数据分组5.1 聚合5.2 转换 六、…