二叉树遍历分为三种:前序、中序和后序,为什么这么命名呢?其实是根据节点顺序命名的。
如图为满节点,1为根节点、2为做节点、3为右节点,主要是看根结点1的位置,在前面就是前序遍历、在中间就是中序遍历、在后面就是后续遍历。
下面我们通过例子,来看看:
- 在遍历节点的左节点的时候,可能左边节点还有左节点和右节点,要一直遍历到叶子节点,才能遍历其右节点
- 例如在遍历到节点2时,它的左节点是3,但是节点3不是叶子节点,此时就不能去遍历节点2的右节点4
- 虽然节点3没有左节点,但是有右节点5,它应该在节点2的右节点4的前面
- 也就是我们在遍历的时候,要将左边节点遍历完才能遍历右边节点
package day03;
/**
* 二叉树的遍历(递归版)
* @author Danycym
*
*/
public class Code01_PreInPosTraversalRecur {
//树节点
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 + " ");
}
public static void main(String[] args) {
Node head = new Node(5);
head.left = new Node(3);
head.right = new Node(8);
head.left.left = new Node(2);
head.left.right = new Node(4);
head.left.left.left = new Node(1);
head.right.left = new Node(7);
head.right.left.left = new Node(6);
head.right.right = new Node(10);
head.right.right.left = new Node(9);
head.right.right.right = new Node(11);
System.out.print("前序遍历:");
preOrderRecur(head);
System.out.print("\n中序遍历:");
inOrderRecur(head);
System.out.print("\n后序遍历:");
posOrderRecur(head);
}
}
上图没执行到最后,后面的按照重复模式执行下去即可
package day03;
import java.util.Stack;
/**
* 二叉树遍历(非递归版)
* @author Danycym
*
*/
public class Code02_PreInPosTraversalUnRecur {
public static class Node{
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
//前序遍历
public static void preOrderUnRecur(Node head) {
if(head != null) {
Stack<Node> stack = new Stack<Node>();
stack.push(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);
}
}
}
}
//中序遍历
public static void inOrderUnRecur(Node head) {
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;
}
}
}
}
//后序遍历1
public static void posOrderUnRecur1(Node head) {
if(head != null) {
Stack<Node> stack = new Stack<Node>();
stack.push(head);
Node c = null;
while(!stack.isEmpty()) {
c = stack.peek();
if(c.left != null && head != c.left && head != c.right) {
stack.push(c.left);
}else if(c.right != null && head != c.right) {
stack.push(c.right);
}else {
System.out.print(stack.pop().value + " ");
head = c;
}
}
}
}
//后序遍历2
public static void posOrderUnRecur2(Node head) {
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 + " ");
}
}
}
public static void main(String[] args) {
Node head = new Node(5);
head.left = new Node(3);
head.right = new Node(8);
head.left.left = new Node(2);
head.left.right = new Node(4);
head.left.left.left = new Node(1);
head.right.left = new Node(7);
head.right.left.left = new Node(6);
head.right.right = new Node(10);
head.right.right.left = new Node(9);
head.right.right.right = new Node(11);
System.out.print("前序遍历:");
preOrderUnRecur(head);
System.out.print("\n中序遍历:");
inOrderUnRecur(head);
System.out.print("\n后序遍历1:");
posOrderUnRecur1(head);
System.out.print("\n后序遍历2:");
posOrderUnRecur2(head);
}
}
现在有一种新的二叉树节点类型如下:
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
的后继节点。
package day03;
public class Code03_SuccessorNode {
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;
}
public static void main(String[] args) {
Node head = new Node(1);
head.parent = null;
head.left = new Node(2);
head.left.parent = head;
head.right = new Node(3);
head.right.parent = head;
head.left.left = new Node(4);
head.left.left.parent = head.left;
head.left.right = new Node(5);
head.left.right.parent = head.left;
head.right.left = new Node(6);
head.right.left.parent = head.right;
head.right.right = new Node(7);
head.right.right.parent = head.right;
head.left.right.left = new Node(8);
head.left.right.left.parent = head.left.right;
head.right.right.left = new Node(9);
head.right.right.left.parent = head.right.right;
head.right.right.right = new Node(10);
head.right.right.right.parent = head.right.right;
Node test = head;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.right.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.right.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.right.right;
System.out.println(test.value + " next: " + getSuccessorNode(test));
}
}
package day03;
import java.util.LinkedList;
import java.util.Queue;
public class Code04_SerializeAndReconstructTree {
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 str) {
String[] values = str.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;
}
//层次遍历序列化
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 str) {
String[] values = str.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;
}
private static Node generateNodeByString(String val) {
if(val.equals("#")) {
return null;
}
return new Node(Integer.valueOf(val));
}
//打印二叉树结构
public static void printTree(Node head) {
System.out.println("二叉树:");
printInOrder(head, 0, "H", 17);
}
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 = null;
printTree(head);
String pre = serialByPre(head);
System.out.println("前序遍历序列化: " + pre);
head = reconByPreString(pre);
System.out.println("前序遍历反序列化:");
printTree(head);
System.out.println("====================================");
head = new Node(1);
printTree(head);
pre = serialByPre(head);
System.out.println("前序遍历序列化: " + pre);
head = reconByPreString(pre);
System.out.println("前序遍历反序列化:");
printTree(head);
System.out.println("====================================");
head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.right.right = new Node(5);
printTree(head);
pre = serialByPre(head);
System.out.println("前序遍历序列化: " + pre);
head = reconByPreString(pre);
System.out.println("前序遍历反序列化:");
printTree(head);
System.out.println("====================================");
head = new Node(1);
head.left = new Node(2);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right = new Node(3);
head.right.left = new Node(6);
head.right.right = new Node(7);
printTree(head);
pre = serialByPre(head);
System.out.println("前序遍历序列化: " + pre);
head = reconByPreString(pre);
System.out.println("前序遍历反序列化:");
printTree(head);
System.out.println("====================================");
head = new Node(1);
head.left = new Node(2);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right = new Node(3);
head.right.left = new Node(6);
head.right.right = new Node(7);
printTree(head);
pre = serialByLevel(head);
System.out.println("层次遍历序列化: " + pre);
head = reconByLevelString(pre);
System.out.println("层次遍历反序列化:");
printTree(head);
System.out.println("====================================");
}
}
请把一段纸条竖着放在桌子上,然后从纸条的下边向上方对折1次,压出折痕后展开。此时 折痕是凹下去的,即折痕突起的方向指向纸条的背面。如果从纸条的下边向上方连续对折2 次,压出折痕后展开,此时有三条折痕,从上到下依次是下折痕、下折痕和上折痕。
给定一 个输入参数N,代表纸条都从下边向上方连续对折N次,请从上到下打印所有折痕的方向。
例如:
程序中,可以通过一个
boolean
类型的变量表示是折痕的左边还是右边
package day03;
public class Code05_PaperFolding {
public static void printAllFolds(int N) {
printProcess(1, N, true);
}
public static void printProcess(int i, int N, boolean down) {
if (i > N) {
return;
}
printProcess(i + 1, N, true);
System.out.print(down ? "down " : "up ");
printProcess(i + 1, N, false);
}
public static void main(String[] args) {
int N = 1;
printAllFolds(N);
System.out.println();
N = 2;
printAllFolds(N);
System.out.println();
N = 3;
printAllFolds(N);
}
}
判断一棵二叉树是否是平衡二叉树,就需要判断每个节点的左子树和右子树是否平衡,且左子树与右子树高度差
那么可以通过递归的方式实现
package day03;
/**
* 判断是否为平衡二叉树
* @author Danycym
*
*/
public class Code06_IsBalanceTree {
public static class Node{
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
//第一种写法
//返回结构
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){
return new ReturnData(false, 0);
}
return new ReturnData(true, Math.max(leftData.h , rightData.h) + 1);
}
//第二种写法
//主函数
public static boolean isBalance(Node head) {
boolean[] res = new boolean[1];
res[0] = true;
getHeight(head, 1, res);
return res[0];
}
//求树高度
public static int getHeight(Node head, int level, boolean[] res) {
if (head == null) {
return level;
}
int lH = getHeight(head.left, level + 1, res);
if (!res[0]) {
return level;
}
int rH = getHeight(head.right, level + 1, res);
if (!res[0]) {
return level;
}
if (Math.abs(lH - rH) > 1) {
res[0] = false;
}
return Math.max(lH, rH);
}
public static void main(String[] args) {
Node head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right.left = new Node(6);
head.right.right = new Node(7);
System.out.println(isB(head));
System.out.println(isBalance(head));
head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.left.left.left = new Node(6);
System.out.println(isB(head));
System.out.println(isBalance(head));
}
}
package day03;
import java.util.Stack;
public class Code07_IsBST {
public static class Node{
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static boolean isBST(Node head) {
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;
}
//打印二叉树
public static void printTree(Node head) {
System.out.println("二叉树为:");
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(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.left = new Node(5);
printTree(head);
System.out.println(isBST(head));
}
}
情况可总结如下:
按层遍历
package day03;
import java.util.LinkedList;
import java.util.Queue;
public class Code08_IsCBT {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
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;
//(leaf && (l != null || r != null)) 叶节点,左孩子或右孩子不为空,返回false
//(l == null && r != null) 情况一,左孩子为空,右孩子不为空
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;
}
// for test -- print tree
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(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.left = new Node(5);
printTree(head);
System.out.println(isCBT(head));
head = new Node(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.right = new Node(5);
printTree(head);
System.out.println(isCBT(head));
}
}
package day03;
public class Code09_CompleteTreeNodeNumber {
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));
}
/**
*
* @param node 当前节点
* @param level 当前节点在第几层
* @param h 树的高度,固定值
* @return 返回节点个数
*/
public static int bs(Node node, int level, int h) {
if (level == h) {
return 1;
}
if (mostLeftLevel(node.right, level + 1) == h) {
return (1 << (h - level)) + bs(node.right, level + 1, h);
} else {
return (1 << (h - level - 1)) + bs(node.left, level + 1, h);
}
}
/**
* 求二叉树的高度
* @param node 当前节点
* @param level 所在层数
* @return 返回二叉树高度
*/
public static int mostLeftLevel(Node node, int level) {
while (node != null) {
level++;
node = node.left;
}
return level - 1;
}
public static void main(String[] args) {
Node head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right.left = new Node(6);
System.out.println(nodeNum(head));
}
}