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