一、基础概念
1.定义
二叉树是每个节点最多有两个子节点的树形结构,两个子节点分别叫:左子节点、右子节点

2. 核心术语
根节点:树最顶端的节点
叶子节点:没有子节点的节点
父节点 / 子节点:上下级关系节点
深度:从根到当前节点的层数
高度:从当前节点到最远叶子的层数
3. 特殊二叉树
满二叉树:所有层都填满节点

完全二叉树:除最后一层外都填满,最后一层靠左排列

二叉搜索树 (BST):左 < 根 < 右,中序遍历是升序

平衡二叉树:左右子树高度差 ≤1

二、定义二叉树节点
这里用java来实现
java
// 二叉树节点定义
public class TreeNode {
int val; // 节点值
TreeNode left; // 左子节点
TreeNode right; // 右子节点
// 构造方法
TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
三、遍历二叉树
1.前序遍历:根-->左-->右
java
//先序遍历
public void preOrder(TreeNode root){
if(root==null) return;
System.out.print(root.val+" ");//根
preOrder(root.left);//左
preOrder(root.right);//右
}
2.中序遍历:左-->根-->右
在上面提到的二叉搜索树是左子树值 < 根节点值 < 右子树值,因此二叉搜索树中序遍历的结果是升序的
java
// 中序遍历
public void inOrder(TreeNode root) {
if (root == null) return;
inOrder(root.left); // 左
System.out.print(root.val + " "); // 根
inOrder(root.right); // 右
}
3.后序遍历:左-->右-->根
java
// 后序遍历
public void postOrder(TreeNode root) {
if (root == null) return;
postOrder(root.left); // 左
postOrder(root.right); // 右
System.out.print(root.val + " "); // 根
}
4.层序遍历:按层遍历(BFS)
用队列实现,这样可以一层一层来访问
java
// 层序遍历(队列实现)
public void levelOrder(TreeNode root) {
if (root == null) return; // 1. 空树直接返回
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root); // 2. 根节点入队
while (!queue.isEmpty()) { // 3. 队列不为空就继续
TreeNode node = queue.poll();// 4. 出队一个节点
System.out.print(node.val + " "); // 5. 访问它
// 6. 左孩子、右孩子依次入队
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}
四、构建二叉树
java
public class BinaryTreeTest {
public static void main(String[] args) {
// 构建如下二叉树:
// 1
// / \
// 2 3
// / \
// 4 5
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.left.right = new TreeNode(5);
}
}
五、求二叉树的高度
这里用递归来实现,不断地求左子树的高度和右子树的高度,然后返回两者最大值加1(1是根节点)
java
public int getHeight(TreeNode root) {
if (root == null) return 0;
int leftH = getHeight(root.left);
int rightH = getHeight(root.right);
return Math.max(leftH, rightH) + 1;
}
六、综合测试代码
java
import java.util.LinkedList;
import java.util.Queue;
// 节点类
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
// 二叉树操作类
public class BinaryTree {
// 前序
public void preOrder(TreeNode root) {
if(root==null)return;
System.out.print(root.val+" ");
preOrder(root.left);
preOrder(root.right);
}
// 中序
public void inOrder(TreeNode root) {
if(root==null)return;
inOrder(root.left);
System.out.print(root.val+" ");
inOrder(root.right);
}
// 后序
public void postOrder(TreeNode root) {
if(root==null)return;
postOrder(root.left);
postOrder(root.right);
System.out.print(root.val+" ");
}
// 层序
public void levelOrder(TreeNode root) {
if(root==null)return;
Queue<TreeNode> q=new LinkedList<>();
q.offer(root);
while(!q.isEmpty()){
TreeNode n=q.poll();
System.out.print(n.val+" ");
if(n.left!=null)q.offer(n.left);
if(n.right!=null)q.offer(n.right);
}
}
// 树高
public int getHeight(TreeNode root) {
if(root==null)return 0;
return Math.max(getHeight(root.left),getHeight(root.right))+1;
}
public static void main(String[] args) {
BinaryTree tree=new BinaryTree();
TreeNode root=new TreeNode(1);
root.left=new TreeNode(2);
root.right=new TreeNode(3);
root.left.left=new TreeNode(4);
root.left.right=new TreeNode(5);
System.out.println("前序:");tree.preOrder(root);
System.out.println("\n中序:");tree.inOrder(root);
System.out.println("\n后序:");tree.postOrder(root);
System.out.println("\n层序:");tree.levelOrder(root);
System.out.println("\n树高:"+tree.getHeight(root));
}
}
七、小舟有话说
这里仅介绍了一些概念和基本算法,下一篇会着重讲一些常见的算法题哦~
如果喜欢的话可以点点关注,下次找我不迷路~