以中序为例,每走一个节点就遍历一遍左子树,为空就返回,打印根节点,遍历右子树。
每个数字走3遍,数字打印的顺序(递归序)决定了是先序/中序/后序
树的高度是从根节点到叶子节点的路径数
只有一个节点的二叉树高度是0
空树高度是-1
平衡二叉树的左子树与右子树的高度差不能超过1
be like:
先序中序后序遍历
public class TraversalBinaryTree {
public static class Node{
public int value;
public Node left;
public Node right;
public Node(int v){
value = v;
}
}
public static void f(Node head){
if(head==null){
return; //如果节点为null,结束方法返回上一层
}
f(head.left); //遍历左节点
f(head.right);//遍历右节点
}
//先序
public static void pre(Node head) {
if(head==null){
return;
}
System.out.println(head.value);
pre(head.left); //扫描左子树
pre(head.right);//扫描右子树
}
//中序
public static void in(Node head){
if(head==null){
return;
}
in(head.left);
System.out.println(head.value);
in(head.right);
}
//后序
public static void pos(Node head){
if(head==null){
return;
}
pos(head.left);
pos(head.right);
System.out.println(head.value);
}
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);
pre(head);
System.out.println("----------");
in(head);
System.out.println("----------");
pos(head);
}
}
判断两棵树结构是否相等
理解:遍历过程中始终都一样(同时为空)
//判断两颗树的结构是否相等
public class Struct {
public static class TreeNode{
public int val;
public TreeNode left;
public TreeNode right;
}
public static boolean isSameTree(TreeNode p,TreeNode q){
//其中一棵树为空
if(p == null ^ q == null){
return false;
}
//两棵树都为空
if(p==null && q == null){
return true;
}
return p.val == q.val && isSameTree(p.left,q.left) && isSameTree(p.right,q.right);
}
}
镜面树
头节点不会破坏镜面关系
左子树的左节点要和右子树的右节点一样
左子树的右节点要和右子树的左节点一样
public class Struct {
public static class TreeNode{
public int val;
public TreeNode left;
public TreeNode right;
}
public static boolean isSymmetric(TreeNode root){
return isMirror(root,root);
}
private static boolean isMirror(TreeNode root, TreeNode root1) {
if(root == null ^ root1 == null){
return false;
}
if(root == null && root1 ==null){
return true;
}
return root.val == root1.val && isMirror(root.left,root1.right) && isMirror(root.right,root1.left);
}
}
非递归遍历二叉树(栈)
后序
public class Tree {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int v) {
value = v;
}
}
//后序遍历postorder traversal
public static void pos(Node h){
//c永远指向栈顶,h永远指向上次打印的节点c
//1.左右都没有处理先处理左(c左压栈)
//2.左处理完(打印左子树说明左子树已经走到了尽头,h在这里还有一个标记的作用),右如果没处理处理右(c右压栈)
//3.以上都不满足弹出并打印(最后处理的一定是根节点)
System.out.println("pos-order:");
//头节点不为空
if(h!=null){
Stack<Node> stack = new Stack<>();//创建一个栈
stack.push(h); //入栈
Node c = null;
while (!stack.empty()){
c= stack.peek(); //c指向栈顶
if(c.left!=null && h!=c.left && h!=c.right){
stack.push(c.left);
}else if(c.right!=null && h!=c.right){ //处理右子树(第二个条件说明c右没被打印过)
stack.push(c.right);
}else{
System.out.print(stack.pop().value+" ");
h=c;//h来到c所在位置
}
}
}
}
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);
pos(head); //4 5 2 6 7 3 1
}
}
中序
//中序遍历
//有左压左,无左进右压左
System.out.println("in-order:");
if (h != null) {
Stack<Node> stack = new Stack<>();
while (!stack.isEmpty() || h != null) { //栈不为空或节点不为空
if (h != null) {
//压栈,并指向下一个左节点
stack.push(h);
h = h.left;
} else {
//走到尽头弹出并打印栈顶。指向右节点
h = stack.pop(); //h指向栈顶
System.out.print(h.value + " ");
h = h.right;
}
}
}
}