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

二叉树的多种遍历方式

在这里插入图片描述

前序遍历(递归实现)

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> list=new ArrayList<>();return prio(root,list);}public List<Integer>    prio(TreeNode a,List<Integer>list){if(a==null)return list;list.add(a.val);prio(a.left,list);prio(a.right,list);return list;}
}

中序遍历(递归实现)

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> list=new ArrayList<>();return  zhong(root,list);}public List<Integer> zhong(TreeNode a,List<Integer>list){if(a==null)return list;zhong(a.left,list);list.add(a.val);zhong(a.right,list);return list;}

}
后序遍历(递归实现)

class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> list=new ArrayList<>();return hou(root,list);}public List<Integer> hou(TreeNode node,List<Integer>list){if(node==null)return list;hou(node.left,list);hou(node.right,list);list.add(node.val);return list;}
}

非递归实现中序遍历

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> list=new ArrayList<>();LinkedList<TreeNode> stack=new LinkedList<>();//栈记录走过的路程//非递归实现while(root!=null||!stack.isEmpty()){if(root!=null){stack.push(root);root=root.left;}else{TreeNode pop=stack.pop();list.add(pop.val);root=pop.right;}}return list;}
}

非递归实现前序遍历

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> list=new ArrayList<>();LinkedList<TreeNode> stack=new LinkedList<>();while(root!=null||!stack.isEmpty()){if(root!=null){list.add(root.val);stack.push(root);root=root.left;}else{TreeNode pop=stack.pop();root=pop.right;}}return list;}
}

后序遍历(非递归实现)

class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> list=new ArrayList<>();LinkedList<TreeNode> stack=new LinkedList<>();TreeNode pop=null;while(root!=null||!stack.isEmpty()){if(root!=null){stack.push(root);root=root.left;}else{TreeNode peek=stack.peek();if(peek.right==null||peek.right==pop){pop=stack.pop();list.add(pop.val);}else{root=peek.right;}}}return list;}
}

力扣——对称二叉树

在这里插入图片描述

判断二叉树是不是对称二叉树

代码实现(递归解法)
class Solution {public boolean isSymmetric(TreeNode root) {return duichen(root.left,root.right); }public boolean duichen(TreeNode left,TreeNode right){if(right==null&&left==null){return true;}if(right==null||left==null){return false;}if(left.val!=right.val){return false;}return duichen(left.left,right.right)&&duichen(left.right,right.left);}
}

迭代解法

力扣——二叉树的最大深度

class Solution {public int maxDepth(TreeNode root) {if(root==null)return 0;if(root.left==null&&root.right==null){return 1;}int a=maxDepth(root.left);int b=maxDepth(root.right);int i=Math.max(a,b);return i+1;}
}

力扣——二叉树的最小深度

import java.util.*;
class Solution {public int minDepth(TreeNode root) {if(root==null)return 0;int a=minDepth(root.left);int b=minDepth(root.right);int i=Math.min(a,b);//要多做一个判断,如果左子树/右子树为0,那么就以不为0的那个为准if(a==0)return b+1;if(b==0)return a+1;return i+1;}
}

力扣-翻转二叉树

class Solution {public TreeNode flipTree(TreeNode root) {fn(root);return root;}public void fn(TreeNode node){if(node==null){return;}TreeNode temp=node.left;node.left=node.right;node.right=temp;fn(node.left);fn(node.right);}
}

二叉搜索树

在这里插入图片描述

http://www.dtcms.com/a/535835.html

相关文章:

  • Vue3 + Electron + Node.js 桌面项目完整开发指南
  • 【Node.js】Node.js 模块系统
  • 古籍影文公开古籍OCR检测数据集VOC格式共计8个文件
  • 网站的对比哪些网站是做免费推广的
  • 网站建设的整体流程有哪些?建筑工程网站建站方案
  • 区块链的密码学基石:沙米尔秘密共享(SSS)数学原理详解
  • 单例模式详解:从基础到高级的八种实现方式
  • 改版网站收费wordpress国人主题
  • web3.0是什么
  • 计网:网络层
  • git学习3
  • HarmonyOS图形图像处理与OpenGL ES实战
  • SunX:以合规正品,重塑Web3交易信任
  • nacos 使用oceanbase(oracle模式)作为数据源
  • 网站排名优化策划网站一个人可以做吗
  • 基于springboot的民宿在线预定平台开发与设计
  • 脚本探索--Spatial HD进行CNV分析
  • 介绍一下Hystrix的“舱壁模式”和“熔断状态机”
  • 基数排序(Radix Sort)算法简介
  • 【C++项目】基于设计模式的同步异步日志系统(前置基础知识)
  • JDK8时间相关类,时间对象都是不可变的
  • Java内存模型(JMM)与JVM内存模型
  • h5响应式网站模板如何做公司自己的网站首页
  • CentOS7 使用 centos-release-scl-rh yum库安装 devtoolset
  • UI自动化测试:Jenkins配置
  • 软件开发公司网站模板网站开发工程师绩效
  • c++中list详解
  • 杨凌美畅用 TDengine 时序数据库,支撑 500 条产线 2 年历史数据追溯
  • 4.Rocky Linux 网络配置
  • <数据集>yolo螺丝螺母识别数据集<目标检测>