二叉樹的前,中,後序及中序遍歷(還有其它變形算法題)

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.判讀二叉樹是否爲對稱二叉樹

在這裏插入圖片描述

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
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);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章