目录
本文的实例代码基于JAVA编写
首先给出节点的数据结构public class TreeNode {
-
int val;
-
TreeNode left;
-
TreeNode right;
-
TreeNode(int x) {
-
val = x;
-
}
-
}
二叉树的遍历
前序遍历
题目参考LeetCode(144)
递归解法:
- 如果二叉树为空,空操作
- 如果二叉树不为空,访问根节点,前序遍历左子树,前序遍历右子树
-
List<Integer> list = new ArrayList<Integer>();
-
public List<Integer> preorderTraversal(TreeNode root) {
-
if (root != null) {
-
list.add(root.val);
-
if (root.left != null) {
-
preorderTraversal(root.left);
-
}
-
if (root.right != null) {
-
preorderTraversal(root.right);
-
}
-
}
-
return list;
-
}
非递归
-
List<Integer> list = new ArrayList<Integer>();
-
public List<Integer> preorderTraversal(TreeNode root) {
-
Stack<TreeNode> stack = new Stack<>();
-
while (root != null || !stack.isEmpty()) {
-
while (root != null) {
-
list.add(root.val);
-
stack.add(root);
-
root = root.left;
-
}
-
if (!stack.isEmpty()) {
-
TreeNode node = stack.pop();
-
root = node.right;
-
}
-
}
-
return list;
-
}
中序遍历
题目参考LeetCode(94)
递归解法:
- 如果二叉树为空,空操作
- 如果二叉树不为空,中序遍历左子树,访问根节点,中序遍历右子树
-
List<Integer> list = new ArrayList<Integer>();
-
public List<Integer> inorderTraversal(TreeNode root) {
-
if (root != null) {
-
if (root.left != null) {
-
inorderTraversal(root.left);
-
}
-
list.add(root.val);
-
if (root.right != null) {
-
inorderTraversal(root.right);
-
}
-
}
-
return list;
-
}
非递归解法:用栈先把根节点的所有左孩子都添加到栈内,然后输出栈顶元素,再处理栈顶元素的右子树。
-
List<Integer> list = new ArrayList<Integer>();
-
public List<Integer> inorderTraversal(TreeNode root) {
-
Stack<TreeNode> stack = new Stack<>();
-
while (root != null || !stack.isEmpty()) {
-
while (root != null) {
-
stack.add(root);
-
root = root.left;
-
}
-
if (!stack.isEmpty()) {
-
root = stack.pop();
-
list.add(root.val);
-
root = root.right;
-
}
-
}
-
return list;
-
}
后序遍历
题目参考LeetCode(145)
递归解法:
- 如果二叉树为空,空操作
- 如果二叉树不为空,后序遍历左子树,后序遍历右子树,访问根节点
-
List<Integer> list = new ArrayList<Integer>();
-
public List<Integer> postorderTraversal(TreeNode root) {
-
if (root != null) {
-
if (root.left != null) {
-
postorderTraversal(root.left);
-
}
-
if (root.right != null) {
-
postorderTraversal(root.right);
-
}
-
list.add(root.val);
-
}
-
return list;
-
}
非递归解法:双栈法。
-
List<Integer> list = new ArrayList<Integer>();
-
public List<Integer> postorderTraversal(TreeNode root) {
-
Stack<TreeNode> stack1 = new Stack<>();
-
Stack<TreeNode> stack2 = new Stack<>();
-
if (root != null) {
-
stack1.add(root);
-
}
-
while (!stack1.isEmpty()) {
-
TreeNode node = stack1.pop();
-
stack2.add(node);
-
if (node.left != null) {
-
stack1.add(node.left);
-
}
-
if (node.right != null) {
-
stack1.add(node.right);
-
}
-
}
-
while (!stack2.isEmpty()) {
-
list.add(stack2.pop().val);
-
}
-
return list;
-
}
层次遍历
思路:分层遍历二叉树(按层次从上到下,从左到右)迭代,相当于广度优先搜索,使用队列实现。队列初始化,将根节点压入队列。当队列不为空,进行如下操作:弹出一个节点,访问,若左子节点或右子节点不为空,将其压入队列。
-
public static void levelTraversal(TreeNode root){
-
if(root == null) {
-
return;
-
}
-
Queue<TreeNode> queue = new LinkedList<>(); // 对列保存树节点
-
queue.add(root);
-
while (!queue.isEmpty()) {
-
TreeNode temp = queue.poll();
-
System.out.print(temp.val + "->");
-
if (temp.left != null) { // 添加左右子节点到对列
-
queue.add(temp.left);
-
}
-
if (temp.right != null) {
-
queue.add(temp.right);
-
}
-
}
-
}
变形:按层保存,题目参考LeetCode(102)
-
public List<List<Integer>> levelOrder(TreeNode root) {
-
List<List<Integer>> list = new ArrayList<List<Integer>>();
-
Queue<TreeNode> queue = new LinkedList<>();
-
if (root != null) {
-
queue.add(root);
-
}
-
while (!queue.isEmpty()) {
-
int size = queue.size();
-
List<Integer> l = new ArrayList<>();
-
while (size > 0) {
-
TreeNode node = queue.poll();
-
if (node.left != null) {
-
queue.add(node.left);
-
l.add(node.left.val);
-
}
-
if (node.right != null) {
-
queue.add(node.right);
-
l.add(node.right.val);
-
}
-
size--;
-
}
-
list.add(l);
-
}
-
return list;
-
}
基础算法
求二叉树中的节点个数
递归解法: O(n)O(n)
- 如果二叉树为空,节点个数为0
- 如果二叉树不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1
-
public static int getNodeNumRec(TreeNode root) {
-
if (root == null) {
-
return 0;
-
}
-
return getNodeNumRec(root.left) + getNodeNumRec(root.right) + 1;
-
}
非递归解法:O(n)O(n)。基本思想同LevelOrderTraversal。即用一个Queue,在Java里面可以用LinkedList来模拟
-
public static int getNodeNum(TreeNode root) {
-
if (root == null) {
-
return 0;
-
}
-
Queue<TreeNode> queue = new LinkedList<>(); // 用队列保存树节点,先进先出
-
queue.add(root);
-
int count = 1; // 节点数量
-
while (!queue.isEmpty()) {
-
TreeNode temp = queue.poll(); // 每次从对列中删除节点,并返回该节点信息
-
if (temp.left != null) { // 添加左子孩子到对列
-
queue.add(temp.left);
-
count++;
-
}
-
if (temp.right != null) { // 添加右子孩子到对列
-
queue.add(temp.right);
-
count++;
-
}
-
}
-
return count;
-
}
求二叉树的深度(高度)
题目参考LeetCode(104)
递归解法: O(n)O(n)
- 如果二叉树为空,二叉树的深度为0
- 如果二叉树不为空,二叉树的深度 = max(左子树深度, 右子树深度) + 1
-
public int maxDepth(TreeNode root) {
-
int d = 0;
-
if (root == null) {
-
return 0;
-
}
-
d = Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
-
return d;
-
}
非递归解法:O(n)O(n)。基本思想同LevelOrderTraversal。即用一个Queue,在Java里面可以用LinkedList来模拟。
-
public static int getDepth(TreeNode root) {
-
if (root == null) {
-
return 0;
-
}
-
int currentLevelCount = 1; // 当前层的节点数量
-
int nextLevelCount = 0; // 下一层节点数量
-
int depth = 0; // 树的深度
-
Queue<TreeNode> queue = new LinkedList<>(); // 对列保存树节点
-
queue.add(root);
-
while (!queue.isEmpty()) {
-
TreeNode temp = queue.remove(); // 移除节点
-
currentLevelCount--; // 当前层节点数减1
-
if (temp.left != null) { // 添加左节点并更新下一层节点个数
-
queue.add(temp.left);
-
nextLevelCount++;
-
}
-
if (temp.right != null) { // 添加右节点并更新下一层节点个数
-
queue.add(temp.right);
-
nextLevelCount++;
-
}
-
if (currentLevelCount == 0) { // 如果是该层的最后一个节点,树的深度加1
-
depth++;
-
currentLevelCount = nextLevelCount; // 更新当前层节点数量并且重置下一层节点数量
-
nextLevelCount = 0;
-
}
-
}
-
return depth;
-
}
求二叉树第k层的节点个数
递归解法: O(n)O(n)
思路:求以root为根的k层节点数目,等价于求以root左孩子为根的k-1层(因为少了root)节点数目 加上以root右孩子为根的k-1层(因为 少了root)节点数目。即:
如果二叉树为空或者k<1,返回0
如果二叉树不为空并且k==1,返回1
如果二叉树不为空且k>1,返回root左子树中k-1层的节点个数与root右子树k-1层节点个数之和
-
public static int getNodeNumKthLevelRec(TreeNode root, int k) {
-
if (root == null || k < 1) {
-
return 0;
-
}
-
if (k == 1) {
-
return 1;
-
}
-
return getNodeNumKthLevelRec(root.left, k - 1) + getNodeNumKthLevelRec(root.right, k - 1);
-
}
求二叉树中叶子节点的个数
递归解法:
- 如果二叉树为空,返回0
- 如果二叉树是叶子节点,返回1
- 如果二叉树不是叶子节点,二叉树的叶子节点数 = 左子树叶子节点数 + 右子树叶子节点数
-
public static int getNodeNumLeafRec(TreeNode root) {
-
if (root == null) {
-
return 0;
-
}
-
if (root.left == null && root.right == null) {
-
return 1;
-
}
-
return getNodeNumLeafRec(root.left) + getNodeNumLeafRec(root.right);
-
}
非递归解法:基于层次遍历进行求解,利用Queue进行。
-
public static int getNodeNumLeaf(TreeNode root){
-
if (root == null) {
-
return 0;
-
}
-
int leaf = 0; // 叶子节点个数
-
Queue<TreeNode> queue = new LinkedList<>();
-
queue.add(root);
-
while (!queue.isEmpty()) {
-
TreeNode temp = queue.poll();
-
if (temp.left == null && temp.right == null) { // 叶子节点
-
leaf++;
-
}
-
if (temp.left != null) {
-
queue.add(temp.left);
-
}
-
if (temp.right != null) {
-
queue.add(temp.right);
-
}
-
}
-
return leaf;
-
}
判断两棵二叉树是否相同的树
递归解法:
- 如果两棵二叉树都为空,返回真
- 如果两棵二叉树一棵为空,另外一棵不为空,返回假
- 如果两棵二叉树都不为空,如果对应的左子树和右子树都同构返回真,其他返回假
-
public static boolean isSameRec(TreeNode r1, TreeNode r2) {
-
if (r1 == null && r2 == null) { // 都是空
-
return true;
-
} else if (r1 == null || r2 == null) { // 有一个为空,一个不为空
-
return false;
-
}
-
if (r1.val != r2.val) { // 两个不为空,但是值不相同
-
return false;
-
}
-
return isSameRec(r1.left, r2.left) && isSameRec(r1.right, r2.right); // 递归遍历左右子节点
-
}
非递归解法:利用Stack对两棵树对应位置上的节点进行判断是否相同。
-
public static boolean isSame(TreeNode r1, TreeNode r2){
-
if (r1 == null && r2 == null) { // 都是空
-
return true;
-
} else if (r1 == null || r2 == null) { // 有一个为空,一个不为空
-
return false;
-
}
-
Stack<TreeNode> stack1 = new Stack<>();
-
Stack<TreeNode> stack2 = new Stack<>();
-
stack1.add(r1);
-
stack2.add(r2);
-
while (!stack1.isEmpty() && !stack2.isEmpty()) {
-
TreeNode temp1 = stack1.pop();
-
TreeNode temp2 = stack2.pop();
-
if (temp1 == null && temp2 == null) { // 两个元素都为空,因为添加的时候没有对空节点做判断
-
continue;
-
} else if (temp1 != null && temp2 != null && temp1.val == temp2.val) {
-
stack1.push(temp1.left); // 相等则添加左右子节点判断
-
stack1.push(temp1.right);
-
stack2.push(temp2.left);
-
stack2.push(temp2.right);
-
} else {
-
return false;
-
}
-
}
-
return true;
-
}
判断二叉树是不是平衡二叉树
递归实现:借助前面实现好的求二叉树高度的函数
- 如果二叉树为空, 返回真
- 如果二叉树不为空,如果左子树和右子树都是AVL树并且左子树和右子树高度相差不大于1,返回真,其他返回假
-
public static boolean isAVLTree(TreeNode root) {
-
if (root == null) {
-
return true;
-
}
-
if (Math.abs(getDepth(root.left) - getDepth(root.right)) > 1) { // 左右子树高度差大于1
-
return false;
-
}
-
return isAVLTree(root.left) && isAVLTree(root.right); // 递归判断左右子树
-
}
求二叉树的镜像
递归实现:破坏原来的树,把原来的树改成其镜像
- 如果二叉树为空,返回空
- 如果二叉树不为空,求左子树和右子树的镜像,然后交换左右子树
-
public static TreeNode mirrorRec(TreeNode root) {
-
if (root == null) {
-
return root;
-
}
-
TreeNode left = mirrorRec(root.right); // 递归镜像左右子树
-
TreeNode right = mirrorRec(root.left);
-
root.left = left; // 更新根节点的左右子树为镜像后的树
-
root.right = right;
-
return root;
-
}
递归实现:不能破坏原来的树,返回一个新的镜像树
- 如果二叉树为空,返回空
- 如果二叉树不为空,求左子树和右子树的镜像,然后交换左右子树
-
public static TreeNode mirrorCopyRec(TreeNode root) {
-
if (root == null) {
-
return root;
-
}
-
TreeNode newRoot = new TreeNode(root.val); // 创建新节点,然后交换左右子树
-
newRoot.left = mirrorCopyRec(root.right);
-
newRoot.right = mirrorCopyRec(root.left);
-
return newRoot;
-
}
判断两个二叉树是否互相镜像
递归解法:与比较两棵二叉树是否相同解法一致(题5),非递归解法省略。
- 比较r1的左子树的镜像是不是r2的右子树
- 比较r1的右子树的镜像是不是r2的左子树
-
public static boolean isMirrorRec(TreeNode r1, TreeNode r2) {
-
if (r1 == null && r2 == null) {
-
return true;
-
} else if (r1 == null || r2 == null) {
-
return false;
-
}
-
if (r1.val != r2.val) {
-
return false;
-
}
-
// 递归比较r1的左子树的镜像是不是r2右子树
-
// 和r1的右子树的镜像是不是r2的左子树
-
return isMirrorRec(r1.left, r2.right) && isMirrorRec(r1.right, r2.left);
-
}
判断是否为二分查找树BST
题目参考LeetCode(98)
递归解法:中序遍历的结果应该是递增的
-
public static boolean isValidBST(TreeNode root, int pre){
-
if (root == null) {
-
return true;
-
}
-
boolean left = isValidBST(root.left, pre);
-
if (!left) {
-
return false;
-
}
-
if(root.val <= pre) {
-
return false;
-
}
-
pre = root.val;
-
boolean right = isValidBST(root.right, pre);
-
if(!right) {
-
return false;
-
}
-
return true;
-
}