二叉树相关基本问题
- 实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式
- 在二叉树中找到一个节点的后继节点
- 二叉树的序列化和反序列化
- 二叉树按层序列化
- 判断一颗二叉树是否是平衡二叉树?
- 判断一棵树是否为搜索二叉树?判断一棵树是否是完全二叉树?
- 已知一颗完全二叉树,求其节点的个数?
(from 左神算法初级班第五节)
1.实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式
1)递归方式实现先序、中序、后序遍历
打印时机不同,实现不同的顺序遍历
- 先序遍历:打印第一次访问的节点
- 中序遍历:打印第二次访问的节点
- 后序遍历:打印第三次访问的节点
代码:
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static void preOrderRecur(Node head) {//先序遍历
if (head == null) {
return;
}
System.out.print(head.value + " ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
public static void inOrderRecur(Node head) {//中序遍历
if (head == null) {
return;
}
inOrderRecur(head.left);
System.out.print(head.value + " ");
inOrderRecur(head.right);
}
public static void posOrderRecur(Node head) {//后序遍历
if (head == null) {
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.print(head.value + " ");
}
2)非递归方式(栈):
先序遍历:压栈
- 先压右孩子再压左孩子
public static void preOrderUnRecur(Node head) {
System.out.print("pre-order: ");
if (head != null) {
Stack<Node> stack = new Stack<Node>();
stack.add(head);
while (!stack.isEmpty()) {
head = stack.pop();
System.out.print(head.value + " ");
if (head.right != null) {//有右先压右
stack.push(head.right);
}
if (head.left != null) {//
stack.push(head.left);
}
}
}
System.out.println();
}
中序遍历:
解释一:
- 如果当前节点为空,从栈拿一个打印,当前节点向右
- 如果当前节点不为空,当前节点压入栈,当前节点往左
解释二: - 如果有左孩子,那么就一次性将所有左孩子全部压入栈中,再逐一弹出
- 如果弹出节点右孩子树中有左孩子,有则再一次性将右孩子树的所有左孩子全部压入栈中。
- 一直重复到栈内没有元素
public static void inOrderUnRecur(Node head) {//非递归方式:中序遍历
System.out.print("in-order: ");
if (head != null) {
Stack<Node> stack = new Stack<Node>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
System.out.print(head.value + " ");
head = head.right;
}
}
}
System.out.println();
}
后序遍历(先左再右,中):
- 使用两个栈,我们可以用一个栈实现先序遍历,先打印中间节点,再打印左子树,最后打印右子树(代码用栈实现时,要先压右,再压左);
- 那我们也可以用一个栈改成先打印中间节点,再打印右子树,最后打印左子树。(用栈时,就是先压左,再压右)
- 然后在打印中间节点的时候,先压入到另一个栈中,等遍历完,才打印。
- 顺序就变成了先左子树遍历,再右子树遍历,最后打印。
代码:
public static void posOrderUnRecur1(Node head) {
System.out.print("pos-order: ");
if (head != null) {
Stack<Node> s1 = new Stack<Node>();
Stack<Node> s2 = new Stack<Node>();
s1.push(head);
while (!s1.isEmpty()) {
head = s1.pop();
s2.push(head);//压入栈中,等待遍历完再打印
if (head.left != null) {//先序用栈实现是先右再压左,所以这里要反过来,先左再右
s1.push(head.left);//先压左
}
if (head.right != null) {
s1.push(head.right);//再压右
}
}
while (!s2.isEmpty()) {//辅助栈,等所有节点遍历完了,再打印
System.out.print(s2.pop().value + " ");
}
}
System.out.println();
}
使用一个栈来实现非递归后序遍历(不要求):
public static void posOrderUnRecur2(Node h) {
System.out.print("pos-order: ");
if (h != null) {
Stack<Node> stack = new Stack<Node>();
stack.push(h);
Node c = null;
while (!stack.isEmpty()) {
c = stack.peek();
if (c.left != null && h != c.left && h != c.right) {
stack.push(c.left);
} else if (c.right != null && h != c.right) {
stack.push(c.right);
} else {
System.out.print(stack.pop().value + " ");
h = c;
}
}
}
System.out.println();
}
2.如何直观的打印一颗二叉树(不要求)
先序,中序还原整棵树(不方便)
例如:还原先序、中序都为1,1,1,1的二叉树(会出现两种情况,所以不方便)
解决方法:
逆时针旋转90°
public class Code_02_PrintBinaryTree {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len) {
if (head == null) {
return;
}
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
public static void main(String[] args) {
Node head = new Node(1);
head.left = new Node(-222222222);
head.right = new Node(3);
head.left.left = new Node(Integer.MIN_VALUE);
head.right.left = new Node(55555555);
head.right.right = new Node(66);
head.left.left.right = new Node(777);
printTree(head);
head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.right.left = new Node(5);
head.right.right = new Node(6);
head.left.left.right = new Node(7);
printTree(head);
head = new Node(1);
head.left = new Node(1);
head.right = new Node(1);
head.left.left = new Node(1);
head.right.left = new Node(1);
head.right.right = new Node(1);
head.left.left.right = new Node(1);
printTree(head);
}
}
3.在二叉树中找到一个节点的后继节点
【题目】 现在有一种新的二叉树节点类型如下:
public class Node { public int value; public Node left; public Node right; public Node parent; public Node(int data) { this.value = data; } }
该结构比普通二叉树节点结构多了一个指向父节点的parent指针。假设有一 棵Node类型的节点组成的二叉树,树中每个节点的parent指针 都正确地指向 自己的父节点,头节点的parent指向null。只给一个在二叉树中的某个节点 node,请实现返回node的后继节点的函数。在二叉树的中序遍历的序列中,node的下一个节点叫作node的后继节点。
后继节点:中序遍历的序列中,一个节点的下一个节点。
前驱节点:中序遍历的序列中,一个节点的上一个节点。
1)方法一(遍历整棵树):
从头结点开始,输出中序遍历,生成序列,观察一个节点的下一个节点
2)方法二:
- 一个节点,如果有右子树,所以它的后继节点就是它的右孩子树的最左节点。
- 如果该节点没有右子树,找哪一个左子树是以该节点为结尾的。
代码:
public static class Node {
public int value;
public Node left;
public Node right;
public Node parent;
public Node(int data) {
this.value = data;
}
}
public static Node getSuccessorNode(Node node) {
if (node == null) {
return node;
}
if (node.right != null) {//如果有右子树
return getLeftMost(node.right);
} else {//没有右子树,找到哪一个的左子树是以该节点为结尾的
Node parent = node.parent;
while (parent != null && parent.left != node) {
node = parent;
parent = node.parent;
}
return parent;
}
}
public static Node getLeftMost(Node node) {//找到右子树的最左节点
if (node == null) {
return node;
}
while (node.left != null) {
node = node.left;
}
return node;
}
4.介绍二叉树的序列化和反序列化
1)先序遍历序列化、反序列化
先序遍历序列化代码:
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static String serialByPre(Node head) {
if (head == null) {
return "#_";
}
String res = head.value + "_";
res += serialByPre(head.left);
res += serialByPre(head.right);
return res;
}
先序遍历反序列化代码:
public static Node reconByPreString(String preStr) {
String[] values = preStr.split("_");
Queue<String> queue = new LinkedList<String>();
for (int i = 0; i != values.length; i++) {
queue.offer(values[i]);
}
return reconPreOrder(queue);
}
public static 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;
}
2)按层序列化、反序列化
按层序列化代码:
public static String serialByLevel(Node head) {
if (head == null) {
return "#_";
}
String res = head.value + "_";
Queue<Node> queue = new LinkedList<Node>();
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 static Node reconByLevelString(String levelStr) {
String[] values = levelStr.split("_");
int index = 0;
Node head = generateNodeByString(values[index++]);
Queue<Node> queue = new LinkedList<Node>();
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 static Node generateNodeByString(String val) {
if (val.equals("#")) {
return null;
}
return new Node(Integer.valueOf(val));
}
5.判断一棵二叉树是否是平衡二叉树
平衡二叉树判断标准:任何一个节点左右子树高度相差不超过1
递归方法代码:
public static class ReturnData{
public boolean isB;
public int h;
public ReturnData(boolean isB,int h) {
this.isB=isB;
this.h=h;
}
}
public static boolean isB(Node head) {
return process(head).isB;
}
public static ReturnData process(Node head) {
if(head==null) {
return new ReturnData(true,0);
}
ReturnData leftData = process(head.left);
if(!leftData.isB) {
return new ReturnData(false,0);
}
ReturnData rightData = process(head.right);
if(!rightData.isB) {
return new ReturnData(false,0);
}
if(Math.abs(leftData.h-rightData.h)>1) {//判断左右子树高度差值是否超过1
return new ReturnData(false,0);
}
return new ReturnData(true,Math.max(leftData.h, rightData.h)+1);//返回当前高度
}
6.判断一棵树是否是搜索二叉树、判断一棵树是否是完全二叉树
1)搜索二叉树:对于任何节点,左子树都比它小,右子树都比它大。
判断搜索二叉树标准(不出现重复节点):二叉树的中序遍历的序列是否依次升序
public static boolean inOrderUnRecur(Node head) {//非递归方式:中序遍历改判断是否为搜索二叉树
System.out.print("in-order: ");
int pre = Integer.MIN_VALUE;
if (head != null) {
Stack<Node> stack = new Stack<Node>();
while (!stack.isEmpty() || head != null) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head = stack.pop();
if(head.value<pre) {
return false;
}
pre = head.value;
head = head.right;
}
}
}
return true;
}
2)完全二叉树:
判断逻辑:按层遍历
- 如果有右孩子没有左孩子,不是完全二叉树;
- 如果不是左右孩子双全,后面遇到的所有节点必须是叶子节点,否则不是完全二叉树。
- 如果遍历一次都没有出现这些情况,就是完全二叉树。
判断完全二叉树代码:
public static boolean isCBT(Node head) {
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<Node>();//队列结构
boolean leaf = false;//是否开启叶子阶段,即往后的所有节点必须是叶子节点
Node l = null;
Node r = null;
queue.offer(head);
while (!queue.isEmpty()) {
head = queue.poll();
l = head.left;
r = head.right;
if ((leaf && (l != null || r != null))
||
(l == null && r != null)) {//出现左孩子为空,右孩子不为空,如果开启叶子阶段,左右孩子都不能有
return false;
}
if (l != null) {//如果左不为空,进队列
queue.offer(l);
}
if (r != null) {//如果右不为空,进队列
queue.offer(r);
} else {//如果左孩子或者右孩子为空,开启叶子阶段
leaf = true;
}
}
return true;
}
7.已知一棵完全二叉树,求其节点的个数
要求:时间复杂度低于O(N),N为这棵树的节点个数
注:不能遍历二叉树,否则时间复杂度就高于O(N)
递归方式求解:
- 满二叉树的高度是h,则节点个数是2h-1个。
- 遍历树的左边界,求出高度h1(代价是O(logN))。
- 遍历右子树的左边界,求出高度h2(代价变成O(logN)2),判断是否与h1相等。
- 如果h1,h2相等,则满二叉树的左子树是满的,左子树节点个数为2h1-1个。然后递归求右子树。
- 如果h1,h2不相等,则先求出右子树的节点数2h2-1个。再递归求左子树的节点数。
代码:
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static int nodeNum(Node head) {
if (head == null) {
return 0;
}
return bs(head, 1, mostLeftLevel(head, 1));
}
public static int bs(Node node, int l, int h) {//l表示在第几层,h表示整个这棵树的深度
if (l == h) {//如果来到最后一层
return 1;//叶子节点
}
if (mostLeftLevel(node.right, l + 1) == h) {//右子树的最左深度到没到整颗树的深度
return (1 << (h - l)) + bs(node.right, l + 1, h);//(左树节点个数加当前节点)+递归求右子树的节点个数
} else {
return (1 << (h - l - 1)) + bs(node.left, l + 1, h);//(右子树节点个数加当前节点个数)+递归求左子树节点个数
}
}
public static int mostLeftLevel(Node node, int level) {//找到树的深度
while (node != null) {
level++;
node = node.left;
}
return level - 1;
}