[模版总结] - 树的基本算法1 - 遍历

news/2024/10/25 3:26:49/

树结构定义

一种非线性存储结构,具有存储“一对多”关系的数据元素集合

种类

  • General Tree
    • Trie
    • B/B+ 树
  • 二叉树
    • 满/完满/完全二叉树
      • 完美BT : 除了叶子结点外所有节点都有两个字节点,每一层都完满填充
      • 完全BT: 除最后一层以外其他每一层都完美填充,最后一层从左到右紧密填充
      • 完满BT:  除了叶子结点外所有节点都有两个字节点
    • 二叉搜索树 BST
      • 平衡BST 
        • 红黑树
        • 伸展树
        • 自平衡二叉查找树AVL
        • 替罪羊树
    • 线索二叉树
    • 霍夫曼树/最优二叉树

二叉树遍历方式

所有二叉树基本遍历时间复杂度均为:O(N), N代表结点数量。

前序遍历 (根左右)

  • 题目:Leetcode 144

递归写法 

class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();dc(root, res);return res;}private void dc(TreeNode root, List<Integer> res) {if (root==null) return;res.add(root.val);dc(root.left, res);dc(root.right, res);}
}

中序遍历 (左根右)

  • 题目:Leetcode 94

递归写法

class Solution {List<Integer> res;public List<Integer> inorderTraversal(TreeNode root) {res = new ArrayList<>();dc(root);return res;}private void dc(TreeNode root) {if (root==null) return;dc(root.left);res.add(root.val);dc(root.right);}
}

后续遍历 (左右根)

  •  题目:Leetcode 145
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> res = new ArrayList<>();dc(root, res);return res;}private void dc(TreeNode root, List<Integer> res) {if (root==null) return;dc(root.left, res);dc(root.right, res);res.add(root.val);}    
}

层级遍历 I - 自上而下

  • 题目:Leetcode 102

树/图类层级遍历直接BFS即可

class Solution {public List<List<Integer>> levelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList<>();if (root==null) return res;Queue<TreeNode> q = new LinkedList<>();q.add(root);while (!q.isEmpty()) {int size = q.size();List<Integer> tmp = new ArrayList<>();for (int i=0; i<size; i++) {TreeNode curr = q.poll();tmp.add(curr.val);if (curr.left!=null) q.add(curr.left);if (curr.right!=null) q.add(curr.right);}res.add(tmp);}return res;}
}

层级遍历 II - 自下而上

  • 题目:Leetcode 107
class Solution {public List<List<Integer>> levelOrderBottom(TreeNode root) {List<List<Integer>> res = new ArrayList<>();if (root==null) return res;Queue<TreeNode> q = new LinkedList<>();q.add(root);while (!q.isEmpty()) {int size = q.size();List<Integer> tmp = new ArrayList<>();for (int i=0; i<size; i++) {TreeNode curr = q.poll();if (curr.left!=null) q.add(curr.left);if (curr.right!=null) q.add(curr.right);tmp.add(curr.val);}res.add(0, tmp);}return res;}
}

ZigZag 遍历

  • 题目:Leetcode 103​​​​​
class Solution {public List<List<Integer>> zigzagLevelOrder(TreeNode root) {List<List<Integer>> res = new ArrayList<>();dfs(root, 0, res);return res;}private void dfs(TreeNode root, int height, List<List<Integer>> res) {if (root==null) return;if (res.size()<=height) res.add(new ArrayList<>());if (height%2==0) {res.get(height).add(root.val);} else {res.get(height).add(0, root.val);}dfs(root.left, height+1, res);dfs(root.right, height+1, res);}
}

一些特别的遍历: 

逐列遍历 

T: O(N + C\times RlogR) , N表示dfs遍历时间复杂度,C表示列数,R表示每一列的行数

  • 题目:Leetcode 314
class Solution {TreeMap<Integer, List<Pair<Integer, Integer>>> colMap;public List<List<Integer>> verticalOrder(TreeNode root) {if (root==null) return new ArrayList<>();colMap = new TreeMap<>();dfs(root, 0, 0);List<List<Integer>> res = new ArrayList<>();for (int idx: colMap.keySet()) {Collections.sort(colMap.get(idx), (a, b) -> {return a.getKey()-b.getKey();});List<Integer> tmp = new ArrayList<>();for (Pair<Integer, Integer> a: colMap.get(idx)) {tmp.add(a.getValue());}res.add(tmp);}return res;}private void dfs(TreeNode root, int row, int col) {if (root==null) return;colMap.putIfAbsent(col, new ArrayList<>());colMap.get(col).add(new Pair<>(row, root.val));dfs(root.left, row+1, col-1);dfs(root.right, row+1, col+1);}
}


http://www.ppmy.cn/news/1207841.html

相关文章

stable diffusion为什么能用于文本到图像的生成

推荐基于稳定扩散(stable diffusion) AI 模型开发的自动纹理工具&#xff1a; DreamTexture.js自动纹理化开发包 - NSDT 稳定扩散获得如此多关注的原因 如果你还没有看过它&#xff1a;稳定扩散是一个文本到图像的生成模型&#xff0c;你可以输入一个文本提示&#xff0c;比如…

案例 | 3D可视化工具HOOPS助力SolidWorks edrawings成功引入AR/VR技术

HOOPS中文网慧都科技是HOOPS全套产品中国地区指定授权经销商&#xff0c;提供3D软件开发工具HOOPS售卖、试用、中文试用指导服务、中文技术支持。http://techsoft3d.evget.com/达索系统SolidWorks面临的挑战 达索系统SolidWorks公司开发和销售三维CAD设计软件、分析软件和产品…

【分布式事务】深入探索 Seata 的四种分布式事务解决方案的原理,优缺点以及在微服务中的实现

文章目录 前言一、XA 模式1.1 XA 模式原理1.2 XA 模式的优缺点及应用场景1.3 Seata XA 模式在微服务中的实现 二、AT 模式2.1 Seata AT 模式原理2.2 AT 模式的脏写问题和写隔离3.3 AT 模式的优缺点3.4 Seata AT 模式在微服务中的实现 三、TCC 模式3.1 TCC 模式原理3.2 Seata 的…

Unity中Shader光照探针的支持

文章目录 前言一、光照探针用在哪怎么用1、光照探针的应用场景2、我们按照以上条件&#xff0c;在Unity中搭建一个相同的环境3、创建光照探针 二、在我们自己的Shader中&#xff0c;实现支持光照探针1、使用常用的 cginc2、在 v2f 中&#xff0c;准备如下变量3、在顶点着色器中…

Thales hsm是什么意思,有什么作用?

Thales HSM是一种硬件安全模块(Hardware Security Module&#xff0c;HSM)&#xff0c;是Thales公司开发的一种安全设备&#xff0c;用于保护和管理密码和数字证书。HSM是一种物理设备&#xff0c;通常用于需要高度安全性的环境中&#xff0c;如政府机构、金融机构、大型企业等…

立体相机标定

相机成像过程中涉及的4个坐标系&#xff1a; 1、世界坐标系&#xff1a;由用户定义的三维世界坐标系&#xff0c;描述物体和相机在真实世界中的位置&#xff0c;原点可以任意选择。 2、相机坐标系&#xff1a;以相机的光心为坐标原点&#xff0c;X轴和Y轴平行于图像坐标系的X轴…

[MT8766][Android12] 系统设置隐藏休眠时间和锁屏选项

文章目录 开发平台基本信息问题描述解决方法 开发平台基本信息 芯片: MT8766 版本: Android 12 kernel: msm-4.19 问题描述 最近开发的一款智能盒子&#xff0c;没有屏幕显示&#xff1b;所以&#xff0c;系统默认设置成永不休眠以及默认不锁屏&#xff1b;但是&#xff0c;…

epoll实现 IO复用

1、epoll实现 IO复用 epoll的提出--》它所支持的文件描述符上限是系统可以最大打开的文件的数目&#xff1b;eg&#xff1a;1GB机器上&#xff0c;这个上限10万个左右。 每个fd上面有callback(回调函数)函数&#xff0c;只有活跃的fd才有主动调用callback&#xff0c;不需要轮询…