1.前序遍历
public static void preOrderTraversal(Node root){
if(root==null){
return;
}
System.out.print(root.val+" ");
preOrderTraversal(root.left);
preOrderTraversal(root.right);
}
2.中序遍历
public static void inOrderTraversal(Node root){
if(root==null){
return;
}
inOrderTraversal(root.left);
System.out.print(root.val+" ");
inOrderTraversal(root.left);
}
private static List<Character> inorderReturnList(Node root){
List<Character> list=new ArrayList<>();
if(root==null){
return list;
}
list.addAll(inorderReturnList(root.left));
list.add(root.val);
list.addAll(inorderReturnList(root.right));
return list;
}
3.后序遍历
public static void postOrderTraversal(Node root){
if(root==null){
return;
}
postOrderTraversal(root.left);
postOrderTraversal(root.right);
System.out.print(root.val+" ");
public List<Integer> postOrderTraversal(Node root){
List<Integer> list=new ArrayList<>();
if(root==null){
return list;
}
list.addAll(postOrderTraversal(root.left));
list.addAll(postOrderTraversal(root.right));
list.add(root.val);
return list;
}
4.层序遍历
public static void levelOrder(Node root){
if(root==null){
return ;
}
Queue<Node> queue=new LinkedList<>();
queue.offer(root);
while(!queue.isEmpty()){
Node front=queue.poll();
System.out.print(front.val+" ");;
if(front.left!=null){
queue.offer(front.left);
}
if(front.right!=null){
queue.offer(front.right);
}
}
}
public List<List<Integer>> levelOrder(Node root){
List<List<Integer>> list=new ArrayList<List<Integer>>();
if(root==null)return list;
Queue<Node> queue=new LinkedList<>();
queue.add(root);
int level=0;
while(!queue.isEmpty()){
list.add(new ArrayList<Integer>());
int count=queue.size();
for(int i=0;i<count;i++){
Node front=queue.remove();
list.get(level).add(front.val);
if(front.left!=null)queue.add(front.left);
if(front.right!=null)queue.add(front.right);
}
level++;
}
return list;
}
5.判断两个二叉树是否完全相同
public class IsSameTree {
public boolean isSameTree(Node p, Node q) {
if (p == null && q == null) {
return true;
}
if (p == null || q == null){
return false;
}
return p.val==q.val&&isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
}
}
6.二叉树的结点个数
public static int getSize(Node root){
if(root==null){
return 0;
}
int left=getSize(root.left);
int right=getSize(root.right);
return left+right+1;
}
7.求二叉树的高度
public static int getHeight(Node root){
if(root==null){
return 0;
}
int left=getHeight(root.left);
int right=getHeight(root.right);
return Math.max(left,right)+1;
}
8.判断两个树是否是镜像树
public boolean isMirrorTree(Node p,Node q){
if(p==null&&q==null){
return true;
}
if(p==null||q==null){
return false;
}
return p.val==q.val&&isMirrorTree(p.left,q.right)&&isMirrorTree(p.right,q.left);
}
9.判读二叉树是否为对称二叉树
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/93c476fdd3da84563e2031cbcb8d6391.png)
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root==null){
return true;
}
return panduan(root.left,root.right);
}
private boolean panduan(TreeNode left, TreeNode right) {
if(left==null&&right==null){
return true;
}
if(left==null||right==null){
return false;
}
return left.val==right.val&&panduan(left.right,right.left)&&panduan(left.left,right.right);
}
}