熟悉前序、中序、后序、层次遍历算法

前序遍历

①递归前序遍历
public static List res = new ArrayList<>();

public static List binaryTreePreOrderByRecursion(TreeNode treeNode) {

    if (treeNode == null) {
        return res;
    }
    preOrder(treeNode);

    return res;
}

public static void preOrder(TreeNode node) {

    if (node == null) {
        return;
    }
    res.add(node.val);

    preOrder(node.left);
    preOrder(node.right);
}

②非递归前序遍历
public static List binaryTreePreOrderByStack(TreeNode treeNode) {
    List res = new ArrayList<>();
    Stack stack = new Stack<>();
    if (treeNode == null) {
        return res;
    }
    stack.push(treeNode);

    while (!stack.isEmpty()) {
        TreeNode cur = stack.pop();
        res.add(cur.val);

        if (cur.right != null) {
            stack.push(cur.right);
        }

        if (cur.left != null) {
            stack.push(cur.left);
        }
    }
    return res;
}

中序遍历

①递归
public static List resInOrder = new ArrayList<>();

public static List binaryTreeInOrderByRecursion(TreeNode node){

    if (node == null){
        return resInOrder;
    }

    processInorder(node);

    return resInOrder;
}

public static void processInorder(TreeNode node){
    if (node == null){
        return;
    }

    processInorder(node.left);
    resInOrder.add(node.val);
    processInorder(node.right);
}

②非递归
public static List binaryTreeInOrderByUnRecursion(TreeNode node) {

    List res = new ArrayList<>();
    
    Stack stack = new Stack<>();
    
    if (node == null) {
        return res;
    }
    
    //以所有左节点为基本
    while (node != null) {
        stack.push(node);
        node = node.left;
    }
    
    while (!stack.isEmpty()) {
    
        //左+中
        TreeNode cur = stack.pop();
        res.add(cur.val);
    
        //右节点
        if (cur.right != null){
            cur = cur.right;
    
            //右节点所有左节点,并以其为根本
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
        }
    }
    return res;
}

后续遍历

递归
static List resPost = new ArrayList<>();

public static List binaryTreePostOrderByRecursion(TreeNode node) {

    if (node == null) {
        return resPost;
    }
    processPost(node);
    return resPost;
}

public static void processPost(TreeNode node) {

    if (node == null) {
        return;
    }
    processPost(node.left);
    processPost(node.right);
    resPost.add(node.val);
}

非递归
public static List binaryTreePostOrderByUnRecursion(TreeNode node) {

    Stack stack = new Stack<>();
    List res = new ArrayList<>();

    if (node == null) {
        return res;
    }
    //根节点
    stack.push(node);
    // 中右左
    while (!stack.isEmpty()) {
        TreeNode cur = stack.pop();
        res.add(cur.val);
        if (cur.left != null) {
            //先入左节点
            stack.push(cur.left);
        }
        if (cur.right != null) {
            //先弹右节点
            stack.push(cur.right);
        }
    }
    //左右中,后续遍历
    Collections.reverse(res);
    return res;

}

层次遍历

Queue 队列

  • offer() 添加元素

  • poll 返回第一个元素,并在队列中删除

  • peek() 返回第一个元素,不删除

  • add()和remove()、element()方法在失败的时候会抛出异常(不推荐)

列表or数组

public ArrayList PrintFromTopToBottom(TreeNode root) {
        
        ArrayList result = new ArrayList();
        
        if(root == null){
            return result;
        }
        ArrayList treeNode = new ArrayList();
        treeNode.add(root);
        
        for(int i=0; i

队列

import java.util.*;

public ArrayList PrintFromTopToBottom(TreeNode root) {
    
    ArrayList result = new ArrayList();
    
    if(root == null){
        return result;
    }
    Queue queue = new LinkedList<>();
    queue.offer(root);
    
    while(!queue.isEmpty()){
        TreeNode node = queue.poll();
        result.add(node.val);   

         if(node.left!=null){
            queue.offer(node.left);
        }
        
         if(node.right!=null){
            queue.offer(node.right);
        }
    }
    return result;
}

你可能感兴趣的:(算法)