[算法入门笔记] 4. 二叉树

本文详细介绍了二叉树的各种遍历方法,包括递归和非递归的先序、中序、后序遍历,以及宽度优先遍历。此外,还讨论了二叉搜索树的验证、完全二叉树、平衡二叉树和满二叉树的判断,以及最近公共祖先的查找算法。这些概念和算法对于理解和操作二叉树至关重要。
摘要由CSDN通过智能技术生成

1. 二叉树的遍历

1.1 二叉树的递归遍历

1.1.1 二叉树先序遍历

public void preorder(Node head) {
	if (head == null) {
		return;
	}
	Visit(head);
	preorder(head.left);
	preorder(had.right);
}

1.1.2 二叉树中序遍历

public void inorder(Node head) {
	if (head == null) {
		return;
	}
	inorder(head.left);
	Visit(head);
	inorder(head.right);
}

1.1.3 二叉树后序遍历

public void postorder(Node head) {
	if (head == null) {
		return;
	}
	postorder(head.left);
	postorder(head.right);
	Visit(head);
}

1.2 二叉树的非递归遍历

1.2.1 二叉树先序遍历

public void preorder(Node head) {
	if (head != null) {
		Stack<Node> stack = new Stack<>();
		// 根结点入栈
		stack.add(head);
		while (!stack.isEmpty()) {
			// 弹出栈中一个结点
			head = stack.pop();
			Visit(head);
			// 先入栈该结点的右孩子
			if (head.right != null) {
				stack.push(head.right);
			}
			// 再入栈该结点的左孩子
			if (head.left != null) {
				stack.push(head.left);
			}
		}
	}
}

1.2.2 二叉树中序遍历

public List<Node> inorder(Node root) {
	List<Node> ans = new ArrayList<>();
	Stack<Node> stack = new Stack<>();
	// 根结点为空,直接返回
	if (root == null) {
		return ans;
	}

	while (!stack.isEmpty() || root != null) {
		while (root != null) {
			stack.push(root);
			root = root.left;
		}
		if (!stack.isEmpty()) {
			root = stack.pop();
			ans.add(root); // Visit(root)
			root = root.right;
		}
	}
	return ans;
}

1.2.3 二叉树先序遍历

public void postorder(Node head) {
	if (head != null) {
		// 定义输入栈
		Stack<Node> stack1 = new Stack<>();
		// 定义输出栈
		Stack<Node> stack2 = new Stack<>();
		stack1,push(head);
		while (!stack1.isEmpty()) {
			// 从stack1取出栈顶元素
			head = satck1.pop();
			// 将元素输入到stack2中
			stack2.push(head);
			if (head.left != null) {
				stack1.push(head.left);
			}
			if (head.right != null) {
				stack1.push(head.right);
			}
		}
		
		while (!stack2.isEmpty()) {
			stack2.pop();
		}
	]
}

3. 二叉树的宽度

public int getWidth(Node head) {
	// 定义哨兵结点
	Node p;
	// 定义队列
	Node[] que = new Node[MAXSIZE];
	// 定义队列指针
	int front = 0, rear = 0;
	// 记录当前最大宽度
	int max =0;
	// 记录当前层次结点数
	int count = 0;
	// 记录当前层次最后一个结点
	int last = 1;

	// 根结点入队
	rear = (rear + 1) % MAXSIZE;
	que[rear] = head;
	
	while (front != rear) {
		front = (front + 1) % MAXSIZE;
		p = que[front];
		// 记录当前所属层次结点数
		count++;
		// 当前哨兵结点的左子树入队
		if (p.left != null) {
			rear = (rear +1) % MAXSIZE;
			que[rear] = p.left;
		}
		// 当前哨兵结点的右子树入队
		if (p.right != null) {
			rear = (rear + 1) % MAXSIZE;
			que[rear] = p.right;
		}
		// 更新层次
		if (front == last) {
			// last指向下一层次最后一个结点
			last = rear;
			if (count > max) {
				// 更新最大结点数
				max = count;
			}
			// 重置结点数
			count = 0;
		}
	}
	return max;
}

3. 二叉搜索树

3.1 方式1 遍历中序序列

public boolean isBST(Node head) {
	// 空树也是BST
	if (head == null) {
		return true;
	}
	List<Node> inList = new LinkedList<>();
	process(head, inList);
	int pre = Integer.MIN_VALUE;
	// 如果中序遍历不是递增的,不是二叉搜索树
	for (Node node : inList) {
		if (pre >= node.value) {
			return false;
		}
		pre = node.value;
	}
	return true;
}

public void process(Node node, List<Node> inList) {
	if (node == null) {
		return;
	}
	process(node.left, inList);
	inList.add(node);
	process(node.right, inList);
}

3.2 方式2 递归直接判断(定义)

int prev = Integer.MIN_VALUE;

public boolean isBST(Node head) {
	// 空树也是BST
	if (head == null) {
		return true;
	}
	// 检查左子树
	boolean isLeftBST = isBST(head.left);
	
	// 如果左子树不符合要求,则返回false
	if (!isLeftBST) {
		return false;
	}
	// 检查当前结点与它的左孩子大小
	if (head.value <= prev) { // 如果当前结点值小于左孩子值,返回false
		return false;
	} else {
		// 更新prev
		prev = head.value;
	}
	
	// 检查其右子树是不是BST
	return isBST(head.right);
}

3.3 方式3 树形dp

[使用场景]
如果题目求解目标S规则,其求解流程定成以每一个结点为头结点的子树在S下的每个答案,并且最终答案一定在其中。
[题解图示]
在这里插入图片描述
[第一步] 以某个结点X为头结点的子树中,分析答案的可能性,并且可能性是以X的左子树、X的右子树和X整棵树的角度来考虑的

  • X左子树是否为BST
  • X右子树是否为BST
  • 以X为根结点的树是否为BST
    [第二步] 根据可能性,列出需要的信息。
  • 左子树是否为BST
  • 左子树的最大值
  • 右子树是否为BST
  • 右子树的最小值
    [第三步] 合并第二步信息,对左子树和右子树提出同样要求,写出信息结构。

信息结构

public class ReturnData {
	// 该子树是否为BST
    public boolean isBST;
    // 该子树中最小值
    public int min;
    // 该子树中最大值
    public int max;

	public ReturnData(boolean isBST, int min, int max) {
	    this.isBST = isBST;
        this.min = min;
        this.max = max;
	}
}

[第四步] 设计递归函数,递归函数处理以X为头结点的情况下的答案,包括设计递归的basecase,默认直接得到左子树和右子树的所有信息,以及把可能性做整合。

设计递归函数

public ReturnData process(Node head) {
	// 如果头结点为空,返回null
	if(head == null) {
	}
	
	// 递归检查左子树
	ReturnData leftData = process(head.left);
	// 递归检查右子树
	ReturnData rightData = process(head.right);
	
	/*核心逻辑*/
	
	// 记录右子树的最小值
	int min = head.value;
	// 记录左子树的最大值
	int max = head.value;
	// 求出左孩子最小值与最大值
	if (leftData != null) {
		min = Math.min(min, leftData.min);
		max = Math.max(max, leftData.max); 
	}
	// 求出右孩子最大值与最小值
	if (rightData != null) {
		min = Math.min(min,rightData.min);
        max = Math.max(max,rightData.max);
	}
	boolean isBST = true;
	// 当左孩子不是BST或左孩子中的最大值 >= head时,返回false
	if (leftData != null && (!leftData.isBST || leftData.max >= head.value)) {
		isBST = false;
	} 
	// 当右孩子不是BST或右孩子中的最小值 <= head时,返回false
	if (rightData != null && (!rightData.isBST || head.value >= rightData.min)) {
		isBST = false;
	}
	// 返回整合后的信息结构
	return new ResultData(isBST, max, min);
}

最终调用

public boolean isBST(Node head) {
	return process(head).BST;
}

4. 完全二叉树

算法思想

  • 按层次遍历二叉树
  • 如果当前节点有右孩子节点,但没有左孩子节点,直接返回false
  • 如果当前节点并不是左右孩子全有,那么之后的节点必须为叶子节点,否则返回false
  • 遍历过程不返回false,遍历结束后返回true
// 1.任意一个节点,有右子树无左子树 false
// 2.在第一个条件不违规的条件下,遇到了第一个左右两个孩子不双全的情况,接下来遇到的所有结点都必须是叶子结点

public boolean isCBT(Node head) {
	// 空树符合要求
	if (head == null) {
		return true;
	}
	List<Node> queue = new LinkedList<>();
	// 定义叶子判断标记
	boolean leaf = false;
	// 定义双指针
	Node lchild = null;
	Node rchile = null;
	// 头结点入队
	queue.add(head);

	while (!queue.isEmpty()) {
		// 弹出队首结点
		head = queue.poll();
		lchild = head.left;
		rchild = head.right;

		// 1.开启叶子判断后,如果后续结点不是叶子节点,则返回false
		// 2.如果有右孩子但无左孩子,则返回false
		if (leaf && (lchild != null || rchild != null) || (lchild == null && rchild != null)) {
			return false;
		} 
		// 左孩子入队
		if (lchild != null) {
            queue.add(lchild);
        }
        // 右孩子入队
        if (rchild != null) {
            queue.add(rchild);
        }
		
		// 第一次遇到不是拥有两个孩子的结点,开启叶子判断
		if (lchild == null || rchild == null) {
			leaf = true;
		}
	}
	return true;
}

5. 平衡二叉树

树形dp

[第一步] 以x为头节点的子树中,分析答案的可能性,以x的左子树、x的右子树和x整棵树角度去分析可能性

  • X的左子树是否平衡
  • X的右子树是否平衡
  • X为头结点的左右子树高度差
  • 上述满足后,则平衡

[第二步] 根据可能性列出需要的信息,本题是要知道左子树是否平衡,右子树是否平衡,以及高度信息
[第三步] 整合信息

整合信息

public class Returndata {
	// 是否平衡
	public boolean isBalanced;
	// 高度 
	public int height;

	public ReturnData(boolean isBalanced, int height) {
		this.isBalanced = isBalanced;
        this.height = height
	}
}

[第四步] 设计递归函数,处理以x为头节点的情况的答案,包括base case,左右子树信息,以及可能性整合,返回信息结构

public ReturnData process(Node x) {
	// 空树满足条件,返回true
	if (x == null) {
		return new ReturnData(true, 0)
	}
	// 检查左子树
	ReturnData leftData = process(x.left);
	// 检查右子树
	ReturnData rightData = process(x.right);

	/*核心逻辑*/
	// 返回树的高度
	int height = Math.max(leftData.height, rightData.height) + 1;
	// 判断平衡性
	boolean isBalanced = leftData.isBalanced && rightData.isBalanced && Math.abs(leftData.height - rightData.height) < 2;
	// 返回整合的信息
	return new ReturnData(isBalanced, height);
}

最终调用

public boolean isBalanced(Node head) {
    return process(head).isBalanced;
}

6. 满二叉树

树形dp

[第一步] 以x为头节点的子树中,分析答案的可能性,以x的左子树、x的右子树和x整棵树角度去分析可能性

[第二步] 根据可能性列出需要的信息,本题是要知道左子树是否平衡,右子树是否平衡,以及高度信息

[第三步] 整合信息

public class ReturnData{
    public int height;
    public int nodes;

    public ReturnData(int height, int nodes) {
        this.height = height;
        this.nodes = nodes;
    }
}

[第四步] 设计递归函数,处理以x为头节点的情况的答案,包括base case,左右子树信息,以及可能性整合,返回信息结构

public ReturnData process(Node x) {
	// 空树返回信息
	if (x == null) {
		return new ReturnData(0, 0);
	}
    
    // 返回左子树信息
    ReturnData leftData = process(x.left);
    // 返回右子树信息
    ReturnData rightData = process(x.right);

    /*核心逻辑*/
    // 求以x为根结点的树的高度
    int height = Math.max(leftData.height, rightData.height) + 1;
    // 求以x为根结点的树的数量
    int nodes = leftData.nodes + rightData.nodes + 1;
	
	// 返回信息结构
	return new ReturnData(height, nodes); 
}

最终调用

public boolean isFBT(Node head) {
	// 空树情况
    if (head == null) {
        return true;
    }
    // 返回信息
    ReturnData data = process(head);
    return data.nodes == (1 << data.height - 1);
}

7. 最近公共祖先

7.1 原始问题

给定一颗二叉树的头节点head,以及这棵树中的两个节点o1和o2,请返回o1和o2的最近公共祖先

算法思想

后序遍历二叉树,当前节点记为cur,假设处理cur左子树返回节点为left,处理右子树时返回节点为right

  • 如果发现cur等于null,或者o1、o2,则返回cur
  • 如果leftright都为空,说明cur整棵子树没有发现过o1或o2,返回null
  • 如果leftright都不为空,说明左子树发现过o1或o2,右子树也发现过o1或o2,说明o1向上与o2向上的过程中,首次在cur相遇,返回cur
  • 如果leftright一个为空,另一个不为空,假设不为空的那个记为node,此时node要么是o1或o2的一个,要么是o1和o2的最近公共祖先,直接返回node
public Node lowestAncestor(Node head, Node o1, Node o2) {
    // 如果发现cur等于null,或者o1、o2,则返回cur
    if (head == null || head == o1 || head == o2) {
        return head;
    }
    // 检查左子树
    Node left = lowestAncestor(head.left, o1, o2);
    // 检查右子树
    Node right = lowestAncestor(head.right, o1, o2);

    // 如果left和right都不为空,说明左子树发现过o1或o2,右子树也发现过o1或o2,
    // 说明o1向上与o2向上的过程中,首次在cur相遇,返回cur
    if (left != null && right != null) {
        return head;
    }

    // 左右两棵树并不都有返回值
    // 如果left和right有一个为空,另一个不为空,假设不为空的那个记为node,此时node要么是o1或o2的一个,要么是o1和o2的最近公共祖先,直接返回node
    // 如果left和right都为空,说明cur整棵子树没有发现过o1或o2,返回null
    return left != null ? left : right;
}

7.2 进阶问题

如果查询操作十分频繁,想办法让单条查询的查询时间减少。

结构1 建立二叉树中每个节点对应的父结点信息 <子结点,父结点>

7.2.1 方法1

[结构1建立记录的过程时间复杂度为O(N)、额外空间复杂度为O(N),进行查询操作时,时间复杂度为O(h),其中,h为二叉树的高度。]

public class Record {
	// 定义哈希表表示 <子结点, 父结点>
    private HashMap<Node, Node> map;

	public Record(Node head) {
		map = new HashMap<>();
		// 如果没有父结点
		if (head != null) {
			map.put(head, null);
		}
		setMap(head);
	}
	// 构造map
	public void setMap(Node x) {
		// 如果结点为空,则返回
		if (x == null) {
			return;
		}
		// 如果左孩子存在,设置左孩子的父结点
		if (x.left != null) {
			map.put(x.left, x);
		}
		// 如果右孩子存在,设置右孩子的父结点
		if (x.right != null) {
			map.put(x.right, x);
		}
		// 递归调用左子树
        setMap(head.left);
        // 递归调用右子树
        setMap(head.right);
	}

	public Node query(Node o1, Node o2) {
        // 存储包括o1结点在内的所有o1结点的祖先结点
        // path表示从结点o1到头结点这条路径上所有结点的和
        HashSet<Node> path = new HashSet<>();
        
        // 填充path
        while (map.containsKey(o1)) {
            path.add(o1);
            //获取o1的父节点
            o1 = map.get(o1);
        }
        // 遍历o2的所有父结点与path中进行核对,找出最近公共祖先
        while (!path.contains(o2)) {
            o2 = map.get(o2);
        }
        return o2;
    }
}

7.2.2 方法2

结构2 直接建立任意两个子结点之间的最近公共祖先记录,加速查询

[如果二叉树的结点数为N],需要记录每两个结点之间的信息,信息条数为N(N-1)/2。因此建立结构的空间复杂度为O(N^2),时间复杂度为O(N^2),单次查询的时间复杂度为O(1)。
建立过程:

  • 1.对二叉树中的每棵子树(一共N棵)都进行步骤2
  • 2.假设子树的头结点为h
    • h所有后代结点h结点的最近公共祖先都是h,并记录下来。
    • h左子树的每个结点h右子树的每个结点的最近公共祖先都是h,并记录下来。
public class Record {
	// 建立任意两个结点之间最近公共祖先的哈希表
	private HashMap<Node, HashMap<Node,Node>> map;
	
	public Record(Node head) {
		map = new HashMap<>();
		initMap(head);
		setMap(head);
	}
	
	public void initMap(Node head) {
		// 如果结点为空,退出
		if (head == null) {
			return;
		}
		map.put(head, new HashMap<>());
		
		initMap(head.left);
		initMap(head.right);
	}
	
	public void setMap(Node head) {
		// 如果结点为空
		if (head == null) {
			return;
		}
		// 根结点下的所有节点构成的哈希表的值部分初始化<h,h>
		// <n, <h, h>>
		headRecord(head.left, head);
		headRecord(head.right, head);
		
		subRecord(head);
		
		setMap(head.left);
		setMap(head.right);
	}
	
	// 将以n为根结点的所有子树的结点和h结点的最近公共祖先标为h
	public void headRecord(Node n, Node h) {
		if (n == null) {
			return;
		]
		// <n, <h,h>>
		map.get(n).put(h, h);
		
		headRecord(n.left, h);
		headRecord(n.right, h);
	}

	// 将head为根结点的整棵树的最近公共祖先标head
	public void subRecord(Node head) {
		// 头结点为空,退出
		if (head == null) {
			return;
		}
		preLeft(head.left, head.right, head);
		
		subRecord(head.left);
		subRecord(head.right);
	}

	// 将h左子树的每个结点的最近公共祖先标为h
	public void preLeft(Node l, Node r, Node h) {
		// h的左孩子r为空
		if (l == null) {
			return;
		}
		preRight(l, r, h);
		
		preLeft(l.left, r, h);
		preLeft(l.right, r, h);
	}
	
	// 将h右子树的每个结点的最近公共祖先标为h
	public void preRight(Node l, Node r, Node h) {
		// h的右孩子r为空
		if (r == null) {
			return;
		}
		// <l,<r,h>>
		// 构建以l为键,<r,h>为值得哈希表,表示r的祖先为h
		map.get(l).put(r, h);
		
		preRight(l, r.left, h);
		preRight(l, r.right, h);
	}
	
	public Node query(Node o1, Node o2) {
		if (o1 == o2) {
			return o1;
		}
		if (map.containsKey(o1)) {
			return map.get(o1).get(o2);
		}
		if (map.containsKey(o2)) {
			return map.get(o2).get(o1);
		}
		return null;
	}
}

7.3 进一步进阶的问题(Tarjan+UnionSet)

给定二叉树的头结点head,同时给定所有想要进行的查询。二叉树的结点数量为N,查询条数为M,请在时间复杂度为O(M+N)内返回所有的查询结果。
[提示:使用]Tarjan算法+并查集可解决二叉树结点间最近公共祖先的批量查询问题
近期较忙,日后更新,可参考

8. 找一个结点中序遍历下的后继结点

public class Node {
    public int value;
    public Node left;
    public Node right;
    public Node parent;

    public Node(int data) {
        this.value = data;
    }
}

8.1 情况1

如果node有右子树,那么后继结点就是右子树上最左边的结点
在这里插入图片描述

8.2 情况2

如果node没有右子树,先看node是不是node父节点的左孩子节点

  • 如果是左孩子节点,那么此时node的父节点是node的后继节点;
    在这里插入图片描述
  • 如果是右孩子节点,就向上寻找node的后继结点,假设向上移动到的节点记为ss的父节点记为p,如果发现sp的左孩子节点,那么节点p就是node的后继结点,否则就一直向上移动
    在这里插入图片描述

8.3 情况3

如果在情况2中一直寻找,都移动到空节点时还是没有发现node的后继节点,说明node根本不存在后继节点
在这里插入图片描述

8.4 算法实现

getNextNode

public Node getNextNode(Node node) {
	// 空结点 返回
	if (node == null) {
        return node;
    }
    // 如果node有右子树,那么后继节点就是右子树上最左边的节点
    if (node.right != null) {
        return getLeftMost(node.right);
    } else {
        // 如果node没有右子树,先看node是不是node父节点的左孩子节点,
        Node parent = node.parent;
        // 如果是右孩子节点,就向上寻找node的后继结点,
        // 假设向上移动到的节点记为s,s的父节点记为p,
        // 如果发现s是p的左孩子节点,那么节点p就是node的后继结点,否则就一直向上移动
        while (parent != null && parent.left != node) {
            node = parent;
            parent = node.parent;
        }
        // 如果是左孩子节点,那么此时node的父节点是node的后继节点;
        return parent;
    }
}

getLeftMost

public Node getLeftMost(Node node) {
    if (node == null) {
        return node;
    }
    while (node.left != null) {
        node = node.left;
    }
    return node;
}

9. 二叉树的序列化和反序列化

[自定义] 二叉树被记录成文件的过程叫序列化,通过文件内容重建原来的二叉树叫反序列化。给定一颗二叉树的头节点head,已知二叉树节点类型为32位整数,设计序列化和反序列化的方案

9.1 先序遍历解决

序列化

public String serialByPre(Node head) {
    if (head == null) {
        return "#!";
    }
    String res = head.value + "!";
    res += serialByPre(head.left);
    res += serialByPre(head.right);
    return res;
}

反序列化

public Node reconByPreString(String preStr) {
    String[] values = preStr.split("!");
    Queue<String> queue = new LinkedList<>();
    for (int i = 0; i != values.length ; i++) {
        queue.offer(values[i]);
    }
    return reconPreOrder(queue);
}

public Node reconPreOrder(Queue<String> queue) {
    String value = queue.poll();
    if (value.equals("#")) {
        return null;
    }
    Node head = new Node(Integer.valueOf(value));
    head.left = reconPreOrder(queue);
    head.right = reconPreOrder(queue);
    return head;
}

9.2 层次遍历解决

序列化

public String serialByLevel(Node head) {
    if (head == null) {
        return "#!";
    }
    String res = head.value + "!";
    Queue<Node> queue = new LinkedList<>();
    queue.offer(head);
    
    while (!queue.isEmpty()) {
        head = queue.poll();
        
        if (head.left != null) {
            res += head.left.value + "!";
            queue.offer(head.left);
        } else {
            res += "#!";
        }
        
        if (head.right != null) {
            res += head.right.value + "!";
            queue.offer(head.right);
        } else {
            res += "#!";
        }
    }
    return res;
}

反序列化

// 利用层次遍历反序列化
public Node reconByLevelString(String levelStr) {
    String[] values = levelStr.split("!");
    int index = 0;
    Node head = generateNodeByString(values[index++]);
    Queue<Node> queue = new LinkedList<>();
    if (head != null) {
        queue.offer(head);
    }
    Node node = null;
    while (!queue.isEmpty()) {
        node = queue.poll();
        node.left = generateNodeByString(values[index++]);
        node.right = generateNodeByString(values[index++]);
        if (node.left != null) {
            queue.offer(node.left);
        }
        if (node.right != null) {
            queue.offer(node.right);
        }
    }
    return head;
}

public Node generateNodeByString(String str) {
    if (str.equals("#")) {
        return null;
    }
    return new Node(Integer.valueOf(str));
}

10. 折纸问题

请把一段纸条竖着放在桌子上,然后从纸条的下边向上方对折1次,压出折痕后展开。
此时折痕是凹下去的,即折痕突起的方向指向纸条的背面。
如果从纸条的下边向上方连续对折2次,压出折痕后展开,此时有三条折痕,从上到下依次是下折痕、下折痕和上折痕。
给定一个输入参数N,代表纸条都从下边向上方连续对折N次。
例如:N=1时,打印:down;N=2时,打印:down down up
在这里插入图片描述

public void printAllFolds(int N) {
    printProcess(1, N, true);
}

public void printProcess(int i, int N, boolean down) {
    if (i > N) {
        return;
    }
    printProcess(i + 1, N, true);
    System.out.println(down ? "down " : "up ");
    printProcess(i + 1, N, false);
}

public static void main(String[] args) {
    int N = 1;
    printAllFolds(N);
}

待更新~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Cyan Chau

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值