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

代码随想录学习摘抄day6(二叉树1-11)

一个朴实无华的目录

  • 种类:
    • 满二叉树和完全二叉树
    • 二叉搜索树
      • 平衡二叉搜索树:
  • 存储方法:可以链式存储(指针),也可以顺序存储(数组)。
  • 遍历方式:
    • 深度优先遍历:先往深走,遇到叶子节点再往回走。
    • 广度优先遍历:一层一层的去遍历。
  • 定义:
  • 题型
    • 二叉树的递归遍历
    • 二叉树的迭代遍历
    • 102.二叉树的层序遍历
    • 107.二叉树的层次遍历 II
    • 199.二叉树的右视图
    • 637.二叉树的层平均值:层序遍历的时候把一层求个总和再取一个均值。
    • 429.N叉树的层序遍历:
    • 515.在每个树行中找最大值
    • 116.填充每个节点的下一个右侧节点指针
    • 117.填充每个节点的下一个右侧节点指针II
    • 104.二叉树的最大深度:在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度
    • 111.二叉树的最小深度

种类:

满二叉树和完全二叉树

1.如果一棵二叉树只有度为0的结点和度为2的结点,并且度为0的结点在同一层上,则这棵二叉树为满二叉树。
2.在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层(h从1开始),则该层包含 1~ 2^(h-1) 个节点。

二叉搜索树

若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
它的左、右子树也分别为二叉排序树

平衡二叉搜索树:

又被称为AVL(Adelson-Velsky and Landis)树,且具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

在这里插入图片描述

存储方法:可以链式存储(指针),也可以顺序存储(数组)。

在这里插入图片描述
在这里插入图片描述
如果父节点的数组下标是 i,那么它的左孩子就是 i * 2 + 1,右孩子就是 i * 2 + 2。

遍历方式:

深度优先遍历:先往深走,遇到叶子节点再往回走。

前序遍历(递归法,迭代法)
中序遍历(递归法,迭代法)
后序遍历(递归法,迭代法)

广度优先遍历:一层一层的去遍历。

层次遍历(迭代法)

定义:

struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

题型

二叉树的递归遍历

// 前序遍历·递归·LC144_二叉树的前序遍历
class Solution {public List<Integer> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<Integer>();preorder(root, result);return result;}public void preorder(TreeNode root, List<Integer> result) {if (root == null) {return;}result.add(root.val);preorder(root.left, result);preorder(root.right, result);}
}
// 中序遍历·递归·LC94_二叉树的中序遍历
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);}
}
// 后序遍历·递归·LC145_二叉树的后序遍历
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> preorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()){TreeNode node = stack.pop();result.add(node.val);if (node.right != null){stack.push(node.right);}if (node.left != null){stack.push(node.left);}}return result;}
}// 中序遍历顺序: 左-中-右 入栈顺序: 左-右
class Solution {public List<Integer> inorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> stack = new Stack<>();TreeNode cur = root;while (cur != null || !stack.isEmpty()){if (cur != null){stack.push(cur);cur = cur.left;}else{cur = stack.pop();result.add(cur.val);cur = cur.right;}}return result;}
}// 后序遍历顺序 左-右-中 入栈顺序:中-左-右 出栈顺序:中-右-左, 最后翻转结果
class Solution {public List<Integer> postorderTraversal(TreeNode root) {List<Integer> result = new ArrayList<>();if (root == null){return result;}Stack<TreeNode> stack = new Stack<>();stack.push(root);while (!stack.isEmpty()){TreeNode node = stack.pop();result.add(node.val);if (node.left != null){stack.push(node.left);}if (node.right != null){stack.push(node.right);}}Collections.reverse(result);return result;}
}

102.二叉树的层序遍历

给你一个二叉树,请你返回其按 层序遍历 得到的节点值。 (即逐层地,从左到右访问所有节点)。

class Solution {
public:vector<vector<int>> levelOrder(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<vector<int>> result;while (!que.empty()) {int size = que.size();vector<int> vec;// 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();vec.push_back(node->val);if (node->left) que.push(node->left);if (node->right) que.push(node->right);}result.push_back(vec);}return result;}
};

107.二叉树的层次遍历 II

给定一个二叉树,返回其节点值自底向上的层次遍历。 (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历)

class Solution {
public:vector<vector<int>> levelOrderBottom(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<vector<int>> result;while (!que.empty()) {int size = que.size();vector<int> vec;for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();vec.push_back(node->val);if (node->left) que.push(node->left);if (node->right) que.push(node->right);}result.push_back(vec);}reverse(result.begin(), result.end()); // 在这里反转一下数组即可return result;}
};

199.二叉树的右视图

给定一棵二叉树,想象自己站在它的右侧,按照从顶部到底部的顺序,返回从右侧所能看到的节点值。
层序遍历的时候,判断是否遍历到单层的最后面的元素,如果是,就放进result数组中,随后返回result就可以了。

class Solution {
public:vector<int> rightSideView(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<int> result;while (!que.empty()) {int size = que.size();for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();if (i == (size - 1)) result.push_back(node->val); // 将每一层的最后元素放入result数组中if (node->left) que.push(node->left);if (node->right) que.push(node->right);}}return result;}
};

637.二叉树的层平均值:层序遍历的时候把一层求个总和再取一个均值。

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组。

class Solution {
public:vector<double> averageOfLevels(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<double> result;while (!que.empty()) {int size = que.size();double sum = 0; // 统计每一层的和for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();sum += node->val;if (node->left) que.push(node->left);if (node->right) que.push(node->right);}result.push_back(sum / size); // 将每一层均值放进结果集}return result;}
};

429.N叉树的层序遍历:

给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。

class Solution {
public:vector<vector<int>> levelOrder(Node* root) {queue<Node*> que;if (root != NULL) que.push(root);vector<vector<int>> result;while (!que.empty()) {int size = que.size();vector<int> vec;for (int i = 0; i < size; i++) {Node* node = que.front();que.pop();vec.push_back(node->val);for (int i = 0; i < node->children.size(); i++) { // 将节点孩子加入队列if (node->children[i]) que.push(node->children[i]);}}result.push_back(vec);}return result;}
};

515.在每个树行中找最大值

在二叉树的每一行中找到最大的值。

class Solution {
public:vector<int> largestValues(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);vector<int> result;while (!que.empty()) {int size = que.size();int maxValue = INT_MIN; // 取每一层的最大值for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();maxValue = node->val > maxValue ? node->val : maxValue;if (node->left) que.push(node->left);if (node->right) que.push(node->right);}result.push_back(maxValue); // 把最大值放进数组}return result;}
};

116.填充每个节点的下一个右侧节点指针

给定一个完美二叉树,其所有叶子节点都在同一层,每个父节点都有两个子节点。二叉树定义如下:

struct Node {
int val;
Node *left;
Node *right;
Node *next;
}
填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL。

初始状态下,所有 next 指针都被设置为 NULL。

class Solution {
public:Node* connect(Node* root) {queue<Node*> que;if (root != NULL) que.push(root);while (!que.empty()) {int size = que.size();// vector<int> vec;Node* nodePre;Node* node;for (int i = 0; i < size; i++) {if (i == 0) {nodePre = que.front(); // 取出一层的头结点que.pop();node = nodePre;} else {node = que.front();que.pop();nodePre->next = node; // 本层前一个节点next指向本节点nodePre = nodePre->next;}if (node->left) que.push(node->left);if (node->right) que.push(node->right);}nodePre->next = NULL; // 本层最后一个节点指向NULL}return root;}
};

117.填充每个节点的下一个右侧节点指针II

class Solution {
public:Node* connect(Node* root) {queue<Node*> que;if (root != NULL) que.push(root);while (!que.empty()) {int size = que.size();vector<int> vec;Node* nodePre;Node* node;for (int i = 0; i < size; i++) {if (i == 0) {nodePre = que.front(); // 取出一层的头结点que.pop();node = nodePre;} else {node = que.front();que.pop();nodePre->next = node; // 本层前一个节点next指向本节点nodePre = nodePre->next;}if (node->left) que.push(node->left);if (node->right) que.push(node->right);}nodePre->next = NULL; // 本层最后一个节点指向NULL}return root;}
};

104.二叉树的最大深度:在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

class Solution {
public:int maxDepth(TreeNode* root) {if (root == NULL) return 0;int depth = 0;queue<TreeNode*> que;que.push(root);while(!que.empty()) {int size = que.size();depth++; // 记录深度for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();if (node->left) que.push(node->left);if (node->right) que.push(node->right);}}return depth;}
};

111.二叉树的最小深度

只有当左右孩子都为空的时候,才说明遍历的最低点了。如果其中一个孩子为空则不是最低点

class Solution {
public:int minDepth(TreeNode* root) {if (root == NULL) return 0;int depth = 0;queue<TreeNode*> que;que.push(root);while(!que.empty()) {int size = que.size();depth++; // 记录最小深度for (int i = 0; i < size; i++) {TreeNode* node = que.front();que.pop();if (node->left) que.push(node->left);if (node->right) que.push(node->right);if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出return depth;}}}return depth;}
};

文章转载自:

http://fvabnJrn.jtmrx.cn
http://Cc1DSBt7.jtmrx.cn
http://N1ckUprr.jtmrx.cn
http://C43RXQCd.jtmrx.cn
http://Mc1a88DW.jtmrx.cn
http://jxLolSd7.jtmrx.cn
http://iazBVKlD.jtmrx.cn
http://7j28Sby4.jtmrx.cn
http://OzGu2d9Z.jtmrx.cn
http://qQ4LW7EG.jtmrx.cn
http://5A8ORiae.jtmrx.cn
http://AJSIQZIR.jtmrx.cn
http://dGIvSpJI.jtmrx.cn
http://wUbZTgZf.jtmrx.cn
http://lMrhlWZU.jtmrx.cn
http://hQLGGaQ7.jtmrx.cn
http://VjS8M4PZ.jtmrx.cn
http://kmuJQ75K.jtmrx.cn
http://b3fP5Hon.jtmrx.cn
http://5PAiHiIY.jtmrx.cn
http://6SsUCbsd.jtmrx.cn
http://BqDNxkA2.jtmrx.cn
http://zGDsYYGe.jtmrx.cn
http://7NbXNsSW.jtmrx.cn
http://hYpn1bam.jtmrx.cn
http://c211uMhG.jtmrx.cn
http://IxcC8A2f.jtmrx.cn
http://Xbc8ChBU.jtmrx.cn
http://5CHLhZJc.jtmrx.cn
http://UKKXrpll.jtmrx.cn
http://www.dtcms.com/a/367733.html

相关文章:

  • 吴恩达机器学习(五)
  • Web 与 Nginx 网站服务:从基础到实践
  • 为什么打印出来的 cJSON type 值和头文件定义的不一样?
  • MySQL子查询的分类讲解与实战
  • 【蓝桥杯选拔赛真题64】C++最大空白区 第十四届蓝桥杯青少年创意编程大赛 算法思维 C++编程选拔赛真题解
  • 企业中团队最常使用的git命令操作
  • MCP 和 Fuction Call 有什么不同
  • 去中心化投票系统开发教程 第一章:区块链基础知识
  • 热门盘点|政务办公移动化:开启政务服务高效协同新时代
  • ICPC Central Russia Regional Contest, 2024
  • (A题|烟幕干扰弹的投放策略)2025年高教杯全国大学生数学建模国赛解题思路|完整代码论文集合
  • 化工行业的设备管理软件应用_HawkEye智能运维平台_璞华大数据
  • 论文介绍:Fast3R,更快的多视图 3D 重建的新范式
  • Java 流程控制:从入门到面试的全方位指南
  • 嵌入式第四十六天(51单片机)
  • Dubbo消费者无法找到提供者问题分析和处理
  • ​​Nginx高性能Web服务器实战:从协议原理到运维优化​​
  • 【ffmepg+ AI 】从mp3歌曲提取伴奏(纯音乐)
  • TreeMap 和 LinkedHashMap原理介绍
  • 手写智能指针:带你彻底搞懂 C++ 内存管理的底层逻辑
  • MySQL中CASE语法规则的详细解析及扩展示例
  • 基于单片机楼宇火灾检测系统/仓库火灾检测报警系统
  • 基础crud项目(前端部分+总结)
  • 从零开始学大模型之预训练语言模型
  • 大语言模型基础-Transformer之上下文
  • 数字签名、数字证书、数字信封的概念与区别
  • 【C语言】深入理解指针(5)
  • 240. 搜索二维矩阵 II
  • 嵌入式基础 -- I²C 信号与位层规则
  • Kafka 开启 SASL_PLAINTEXT 双监听器认证(内网/外网)