
以中序为例,每走一个节点就遍历一遍左子树,为空就返回,打印根节点,遍历右子树。
每个数字走3遍,数字打印的顺序(递归序)决定了是先序/中序/后序
先序中序后序遍历
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);
}
}