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

树结构定义

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

种类

  • 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.hqwc.cn/news/166636.html

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

相关文章

VUE获取当前日期的周日和周六

<template><div><div click"handleLast()">上一周</div><div click"handleNext()">下一周</div><el-calendarref"monChild"v-model"value":first-day-of-week"7":range"[sta…

二十、W5100S/W5500+RP2040树莓派Pico<MQTT连接阿里云控制板载LED>

文章目录 1. 前言2. 简介2.1 初步了解阿里云物联网平台创建产品步骤2.2 阿里云物模型讲解 3 WIZnet以太网芯片4 示例概述4.1 流程图4.2 准备工作核心4.3 连接方式4.4 主要代码概述4.5 结果演示 5 注意事项6 相关链接 1. 前言 物联网平台提供安全可靠的设备连接通信能力&#xf…

【uniapp】签名组件,兼容vue2vue3

网上找了个源码改吧改吧&#xff0c;清除了没用的功能和兼容性&#xff0c;基于uniapp开发的 样子 vue2 使用方法&#xff0c;具体的可以根据业务自行修改 <signature ref"signature" width"100%" height"410rpx"></signature>confi…

Python---列表的循环遍历,嵌套

循环遍历就是使用while或for循环对列表中的每个数据进行打印输出 while循环&#xff1a; list1 [貂蝉, 大乔, 小乔]# 定义计数器 i 0 # 编写循环条件 while i < len(list1):print(list1[i])# 更新计数器i 1 for循环&#xff08;推荐&#xff09;&#xff1a; list1 [貂…

定义无向加权图,并使用Pytorch_geometric实现图卷积

首先定义无向边并定义边的权重 import torch import torch.nn as nn from torch_geometric.nn import GCNConv import torch.nn.functional as F from torch_geometric.data import Dataa torch.LongTensor([0, 0, 1, 1, 2, 2, 3, 4]) b torch.LongTensor([0, 1, 2, 3, 1, 5,…

高能数造电池3D打印智能制造小试线,开启全固态电池数字化新时代

在科技创新的浪潮中&#xff0c;电池制造领域又迎来了一次突破性的进展。近日&#xff0c;高能数造(西安)技术有限公司重磅推出了其最新电池数字制造装备——全固态电池3D打印智能制造小试线 &#xff0c;这一创新性的技术开启了全固态电池的数字化智造新时代&#xff0c;为全固…

scitb包1.5版本发布—增加了统计值的结果和自动判断数据是否正态分布的功能

目前&#xff0c;本人写的scitb包1.5版本已经正式在R语言官方CRAN上线&#xff0c;scitb包是一个为生成专业化统计表格而生的R包。目前只能绘制基线表一。 可以使用以下代码安装 install.packages("scitb")安装过旧版本的从新安装一次就可以升级了 scitb包1.5版本修…

微信小程序案例3-2 计算器

文章目录 一、运行效果二、知识储备&#xff08;一&#xff09;data-*自定义属性&#xff08;二&#xff09;模块 三、实现步骤&#xff08;一&#xff09;准备工作&#xff08;二&#xff09;实现页面结构&#xff08;三&#xff09;实现页面样式&#xff08;四&#xff09;实…

笔记本电脑的麦克风没有声音

笔记本电脑的麦克风没有声音是一个常见的问题&#xff0c;可能是由于以下几个原因导致的&#xff1a; 第一&#xff0c;麦克风没有启用或者被禁用了。在Windows系统中&#xff0c;右键单击任务栏上的音量图标&#xff0c;选择“录音设备”&#xff0c;在弹出窗口中找到麦克风&a…

科技云报道:数智化升级,如何跨越数字世界与实体产业的鸿沟?

科技云报道原创。 数智化是当下商业环境下最大的确定性。 2022年&#xff0c;中国数字经济规模达50.2万亿元&#xff0c;占国内生产总值比重提升至41.5%&#xff0c;数字经济成为推动经济发展的重要引擎。从小型创业公司到跨国巨头&#xff0c;数字化转型在企业发展历程中彰显…

LeetCode_多源 BFS_中等_2258.逃离火灾

目录 1.题目2.思路3.代码实现&#xff08;Java&#xff09; 1.题目 给你一个下标从 0 开始大小为 m x n 的二维整数数组 grid &#xff0c;它表示一个网格图。每个格子为下面 3 个值之一&#xff1a; 0 表示草地。1 表示着火的格子。2 表示一座墙&#xff0c;你跟火都不能通过…

【数据结构】二叉树顺序存储:堆详解!(图解+源码)

&#x1f3a5; 屿小夏 &#xff1a; 个人主页 &#x1f525;个人专栏 &#xff1a; 数据结构解析 &#x1f304; 莫道桑榆晚&#xff0c;为霞尚满天&#xff01; 文章目录 &#x1f324;️前言&#x1f324;️堆的理论☁️二叉树的顺序存储☁️堆的概念 &#x1f324;️堆的实现…