当前位置:网站首页>二叉树 | 代码随想录学习笔记
二叉树 | 代码随想录学习笔记
2022-08-10 22:23:00 【Begonia_cat】
跟随carl代码随想录刷题
语言:python
本文为笔者学习二叉树的笔记,主要内容来自于代码随想录。
二叉树
二叉树的定义
二叉树的定义与链表差不多,只不过二叉树的节点里多了两个指针:指向左右孩子。
class TreeNode:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
二叉树的形式、平衡二叉树、二叉搜索树
- 二叉树主要有两种
形式【不包含数值】:- 满二叉树
- 一棵二叉树只有
度为0的节点和度为2的节点,并且度为0的节点在同一层上。 - 深度为
k时,有2^k - 1个节点。
- 一棵二叉树只有
- 完全二叉树
- 除了最底层节点可能没填满外,其余每层节点数都达到了最大值。并且最下面一层的节点都集中在该层
最左边的若干位置。若最底层为n层,则该层包含1~2^(h-1)个节点。
堆就是一个完全二叉树
- 除了最底层节点可能没填满外,其余每层节点数都达到了最大值。并且最下面一层的节点都集中在该层
- 满二叉树
- 二叉搜索树
二叉搜索树是有序树- 如果左子树不为空,则左子树上所有节点的值均
小于它的根节点的值。 - 如果右子树不为空,则右子树上所有节点的值均
大于它的根节点的值。
- 平衡二叉树(又称AVL树)
- 是
空树或左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树
红黑树是一种平衡二叉树 - 一定要知道自己所用语言的
常用的容器底层是如何实现的
- 是
二叉树的存储方式
- 两种方式:
链式存储- 用指针
- 元素散落在内存中的各个地址

顺序存储- 用数组
- 元素在内存中是连续分布的

如果父节点的数组下标是
i,那么它的左孩子就是i*2+1,右孩子就是i*2+2链式表示方式便于理解,一般都用链式存储二叉树(所以喔,
数组依然可以表示二叉树)。
二叉树的遍历方式
- 主要有两种遍历方式:
深度优先遍历- 先往深走,遇到叶子节点再往回走
- ️借助
栈实现【栈是递归的一种实现结构】 - 按照
中间节点的遍历顺序,可细分为三种遍历方式:- 前序遍历(递归法,迭代法)
中左右 - 中序遍历(递归法,迭代法)
左中右 - 后序遍历(递归法,迭代法)
左右中
前序、中序、后序递归遍历代码
前序、中序、后序迭代遍历代码
- 前序遍历(递归法,迭代法)
广度优先遍历一层一层地去遍历
️借助
队列实现层次遍历(迭代法)
边栏推荐
- How to be a Righteous Hacker?What should you study?
- Power system power flow calculation (Newton-Raphson method, Gauss-Seidel method, fast decoupling method) (Matlab code implementation)
- JS学习 2022080
- Nodes in the linked list are flipped in groups of k
- fme csmapreprojector转换器使用高程异常模型进行高程基准转换
- 解码2022中国网安强星丨正向建、反向查,华为构建数字化时代的网络安全防线
- CIKM2022 | Sequence Recommendation Based on Bidirectional Transformers Contrastive Learning
- 华为HCIE云计算之Fusion Access桌面云
- 基于交流潮流的电力系统多元件N-k故障模型研究(Matlab代码实现)【电力系统故障】
- JS中使用正则表达式g模式和非g模式的区别
猜你喜欢

LeetCode每日两题02:反转字符串中的单词 (均1200道)

Black cats take you learn Makefile article 13: a Makefile collection compile problem

leetcode:357. 统计各位数字都不同的数字个数

BM7 list entry in central

What is Jmeter? What are the principle steps used by Jmeter?

LabVIEW分配多少线程?

这款可视化工具神器,更直观易用!太爱了

How does the Weiluntong touch screen display the current value of abnormal data while alarming?

Leave a message with a prize | OpenBMB x Tsinghua University NLP: The update of the large model open class is complete!

Service - DNS forward and reverse domain name resolution service
随机推荐
gcc492 compile `.rodata‘ can not be used when making a PIE object; recompile with -fPIE
Nodes in the linked list are flipped in groups of k
Lambda
STL-stack
What is Jmeter? What are the principle steps used by Jmeter?
DC-9靶场下载及渗透实战详细过程(DC靶场系列)
Addition of linked lists (2)
What would happen if disconnecting during the process of TCP connection?
分享一个后台管理系统可拖拽式组件的设计思路
BM13 determines whether a linked list is a palindrome
pytorch tear CNN
Distribution Network Expansion Planning: Consider Decisions Using Probabilistic Energy Production and Consumption Profiles (Matlab Code Implementation)
元宇宙社交应用,靠什么吸引用户「为爱发电」?
过滤器
Qualcomm Platform Development Series Explanation (Application) Introduction to QCMAP Application Framework
LeetCode Daily Question (1573. Number of Ways to Split a String)
罗克韦尔AB PLC RSLogix5000中计数器指令使用方法介绍
常用代码扩展点设计方式
LeetCode Daily 2 Questions 02: Reverse the words in a string (1200 each)
Fatal error: cstring: No such file or directory