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

二叉树小结

二叉树

树的遍历(如何遍历,如何利用特性问题)

前序遍历(中前后)

递归

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();inorder(root, res);return res;}void inorder(TreeNode root, List<Integer> list) {if (root == null) {return;}inorder(root.left, list);list.add(root.val);             // 注意这一句inorder(root.right, list);}
}

迭代

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

中序遍历(前中后)

递归

class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();inorder(root, res);return res;}void inorder(TreeNode root, List<Integer> list) {if (root == null) {return;}inorder(root.left, list);list.add(root.val);             // 注意这一句inorder(root.right, list);}
}

迭代

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

后序遍历(前后中)

递归

class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();postorder(root, res);return res;}void postorder(TreeNode root, List<Integer> list) {if (root == null) {return;}postorder(root.left, list);postorder(root.right, list);list.add(root.val);             // 注意这一句}
}

迭代

class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();if (root == null) return res;Stack<TreeNode> stack = new Stack<>();stack.push(root);while(!stack.isEmpty()){TreeNode cur = stack.pop();res.add(cur.val);if(cur.left != null) stack.push(cur.left);if(cur.right != null) stack.push(cur.right);}Collections.reverse(res);return res;}
}

利用遍历的特性

  • 涉及到二叉树的构造,无论普通二叉树还是二叉搜索树一定前序,都是先构造中节点。
  • 求普通二叉树的属性,一般是后序,一般要通过递归函数的返回值做计算。
  • 求二叉搜索树的属性,一定是中序了,要不白瞎了有序性了。
http://www.lryc.cn/news/120112.html

相关文章:

  • vue二进制下载
  • c++QT文件操作
  • Jmeter —— jmeter设置HTTP信息头管理器模拟请求头
  • vue 图片转pdf
  • 20.5 HTML 媒体
  • 科大讯飞分类算法挑战赛2023的一些经验总结
  • 2023年京东按摩仪行业数据分析(京东销售数据分析)
  • 【C语言】进阶指针,超详解,含丰富代码示例
  • wireshark入门指北
  • 18、SQL注入之堆叠及WAF绕过注入
  • nodejs+vue+elementui+express旅游出行指南网站_655ms
  • 【心电图信号压缩】ECG信号压缩与通过三次样条近似重建的ECG信号压缩研究(Matlab代码实现)
  • matlab使用教程(11)—创建随机数
  • 一、安全世界观
  • 爬虫014_文件操作_打开关闭_读写_序列化_反序列化---python工作笔记033
  • 企业前后端分离软件架构如何设计?
  • 生产执行MES系统:提升企业灵活性和响应速度的关键利器
  • 什么是分布式系统,如何学习分布式系统
  • 数据库锁表 Lock wait timeout exceeded; try restarting transaction
  • Oracle 知识篇+分区表上的索引由global改为local注意事项
  • 基于2.4G RF开发的无线游戏手柄解决方案
  • Python之一:基础信息
  • K8S系列文章之 Traefik快速入门
  • RabbitMQ在CentOS下的安装
  • 为什么金鸣识别不做成离线版?
  • 什么是面向对象
  • 记一次前端直接上传图片到oss报错
  • 数据库管理-第九十八期 统计信息是多么重要(20230812)
  • 山西电力市场日前价格预测【2023-08-13】
  • AtCoder Beginner Contest 313D题题解