当前位置:网站首页>浅谈二叉树
浅谈二叉树
2022-06-25 09:39:00 【Fly upward】
目录
1.树型结构

1.1 定义
1.2 树的表示形式
class Node {
int value; // 树中存储的数据
Node firstChild; // 第一个孩子引用
Node nextBrother; // 下一个兄弟引用
}
1.3 树的应用
多用于文件系统管理:目录和文件
2.二叉树定理
2.1 概念
二叉树的特点:
2.2 两种特殊的二叉树


2.2 二叉树的性质

2.3 二叉树的存储
// 孩子表示法
class Node {
int val; // 数据域
Node left; // 左孩子的引用,常常代表左孩子为根的整棵左子树
Node right; // 右孩子的引用,常常代表右孩子为根的整棵右子树
}
// 孩子双亲表示法
class Node {
int val; // 数据域
Node left; // 左孩子的引用,常常代表左孩子为根的整棵左子树
Node right; // 右孩子的引用,常常代表右孩子为根的整棵右子树
Node parent; // 当前节点的根节点
}
2.6 二叉树的遍历
1. 前序遍历(Preorder Traversal 亦称先序遍历)——根结点--->根的左子树--->根的右子树。2. 中序遍历(Inorder Traversal)——根的左子树--->根节点--->根的右子树。3. 后序遍历(Postorder Traversal)——根的左子树--->根的右子树--->根节点。
第一颗二叉树
第二课二叉树
在前、中、后遍历中,使用递归方法都是要打印根。
1)前序遍历
解法一
void preOrder(BTNode root) {
if(root == null) {
return;
}
System.out.print(root.val + " ");
preOrder(root.left);
preOrder(root.right);
}
解法二:非递归法
void preorderNor(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
while (cur != null || stack.isEmpty()) {
while (cur != null) {
//进栈
stack.push(cur);
//将栈元素进顺序表
ret.add(cur.val);
cur = cur.left;
}
TreeNode top = stack.pop();
cur = top.right;
}
return ret;
}
2)中序遍历
解法一
void inOrder(BTNode root) {
if(root == null) {
return;
}
inOrder(root.left);
System.out.print(root.val + " ");
inOrder(root.right);
}
解法二:非递归法
void inorderNor(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
while (cur != null || stack.isEmpty()) {
while (cur != null) {
//进栈
stack.push(cur);
cur = cur.left;
}
TreeNode top = stack.pop();
//将栈顶元素进顺序表
ret.add(top.val);
cur = top.right;
}
return ret;
}
3)后序遍历
解法一
//后序遍历
void postOrder(BTNode root) {
if(root == null) {
return;
}
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val + " ");
}
解法二 :非递归法
void inorderNor(TreeNode root) {
List<Integer> ret = new ArrayList<>();
Stack<TreeNode> stack = new Stack();
TreeNode cur = root;
TreeNode prev = null;
while (cur != null || stack.isEmpty()) {
while (cur != null) {
//进栈
stack.push(cur);
cur = cur.left;
}
//读取栈顶元素
TreeNode top = stack.peek();
if () {
stack.pop();
ret.add(top.val);
prev = top;
}else {
cur = top.right;
}
}
return ret;
}
注意:
知道先序遍历和 中序遍历,找后序遍历:
先从先序遍历最前面拿到根,然后拿着根在中序遍历的序列中找到此根,根的左边是左子树,根的右边是右子树。依次循环。
知道后序遍历和 中序遍历,找后序遍历:
先从后序遍历最后面拿到根,然后拿着根在中序遍历的序列中找到此根,根的左边是左子树,根的右边是右子树。依次循环。
2.7 层序遍历
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> ret = new ArrayList<>();
if (root == null) return ret;
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty) {
int size = queue.size();//当前层有多少个节点
List<Integer> lst = new ArrayList<>();
while (size != 0) {
TreeNode cur = queue.poll();
list.add(cur.val);
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
size--;
}
ret.add(list);
}
return ret;
}
边栏推荐
- The way that flutter makes the keyboard disappear (forwarding from the dependent window)
- tokenizers>=0.11.1,!=0.11.3,<0.13 is required for a normal functioning of this module,
- Guiding principle - read source code
- Houdini图文笔记:Could not create OpenCL device of type (HOUDINI_OCL_DEVICETYPE)问题的解决
- 8. Intelligent transportation project (1)
- ‘Flutter/Flutter. h‘ file not found
- 【历史上的今天】6 月 24 日:网易成立;首届消费电子展召开;世界上第一次网络直播
- 什么是 CRA
- Webapi performance optimization
- Redis (I) principle and basic use
猜你喜欢
Jetpack compose layout (II) - material components and layout
Get started quickly with jetpack compose Technology
String implementation strstr()
Bitmap is converted into drawable and displayed on the screen
How do wechat sell small commodity programs do? How to open wechat apps to sell things?
Linked list delete nodes in the linked list
Houdini图文笔记:Could not create OpenCL device of type (HOUDINI_OCL_DEVICETYPE)问题的解决
Jetpack compose layout (III) - custom layout
Flask博客实战 - 实现侧边栏文章归档及标签
Etcd tutorial - Chapter 4 etcd cluster security configuration
随机推荐
Requirements and precautions for applying for multi domain SSL certificate
字符串 实现 strStr()
How do wechat applets make their own programs? How to make small programs on wechat?
SparseArray details
Fluent creates, reads and writes JSON files
【论文阅读|深读】LINE: Large-scale Information Network Embedding
Methodchannel of flutter
Floating window --- create an activity floating window (can be dragged)
Computational Thinking and economic thinking
Houdini图文笔记:Could not create OpenCL device of type (HOUDINI_OCL_DEVICETYPE)问题的解决
什么是 CRA
Guiding principle - read source code
Cocopod error failed: undefined method `map 'for nil:nilclass
Flask博客实战 - 实现侧边栏文章归档及标签
Exception: gradle task assemblydebug failed with exit code 1
Grabcut image segmentation in opencv
Etcd tutorial - Chapter 4 etcd cluster security configuration
How much does a small program cost? How much does a small program cost? It's clear at a glance
Can two Mitsubishi PLC adopt bcnettcp protocol to realize wireless communication of network interface?
An auxiliary MVP architecture project quick development library -mvpfastdagger