Recorrido de árbol binario [algoritmo de aprendizaje]

Prefacio

2023-8-7 15:13:50

El siguiente contenido se deriva de "[Plantilla creativa cinco]"
y está destinado únicamente a fines de aprendizaje y comunicación.

derechos de autor

Elimine las siguientes palabras cuando publique en otras plataformas.
Este artículo se publicó por primera vez en la plataforma CSDN.
El autor es CSDN@日星月云.
La página de inicio del blog es https://blog.csdn.net/qq_51625007.
Elimine las siguientes palabras palabras anteriores al publicar en otras plataformas.

recomendar

Capítulo 6 Árbol [Estructura de datos y algoritmo]

Recorrido de árbol binario [algoritmo de aprendizaje]

recorrido de preorden

144. Recorrido previo al pedido del árbol binario

Implementación recursiva

class Solution {
    
    
    public List<Integer> preorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        preorderTraversal(root,res);
        return res;
    }

    public void preorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        if(root!=null){
    
    
            list.add(root.val);
            preorderTraversal(root.left,list);
            preorderTraversal(root.right,list);      
        }
    }
}

No implementación recursiva -1

class Solution {
    
    
    public List<Integer> preorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        preorderTraversal(root,res);
        return res;
    }

    public void preorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
    
    
            while(cur!=null){
    
    
                list.add(cur.val);
                stack.push(cur);
                cur=cur.left;
            }
            if(!stack.isEmpty()){
    
    
                cur=stack.pop();
                cur=cur.right;
            }
        }
    }
}

No implementación recursiva-2

class Solution {
    
    
    public List<Integer> preorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        preorderTraversal(root,res);
        return res;
    }

    public void preorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
    
    
            if(cur!=null){
    
    
                list.add(cur.val);
                stack.push(cur);
                cur=cur.left;
            }else{
    
    
                cur=stack.pop();
                cur=cur.right;
            }
        }
    }
}

recorrido en orden

94. Recorrido en orden del árbol binario

Implementación recursiva

class Solution {
    
    
    //递归
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        inorderTraversal(root,res);
        return res;
    }
    public void inorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        if(root!=null){
    
    
            inorderTraversal(root.left,list);
            list.add(root.val);
            inorderTraversal(root.right,list);
        }
    
    }

}

Implementación no recursiva-1

class Solution {
    
    
   
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        List<Integer> list=new ArrayList<>();
        Stack<TreeNode> stack=new Stack();
        TreeNode cur=root;
        while(cur!=null||!stack.isEmpty()){
    
    //当前结点指针及栈均空,则结束
            while(cur!=null){
    
    //访问根结点,根指针进栈,进入左子树
                stack.push(cur);
                cur=cur.left;
            }
            if(!stack.isEmpty()){
    
    
                cur=stack.pop();
                list.add(cur.val);
                cur=cur.right;//根指针退栈,进入其右子树
            }
        }

        return list;
    }
   


}

Implementación no recursiva-2

class Solution {
    
    
    //递归
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        inorderTraversal(root,res);
        return res;
    }
    public void inorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root;
        //结点不为空或栈不为空
        while(cur!=null||!stack.isEmpty()){
    
    
            //结点不为空
            if(cur!=null){
    
    
                stack.push(cur);
                cur=cur.left;
            }else{
    
    
                cur=stack.pop();
                list.add(cur.val);
                cur=cur.right;
            }
        }
    
    }

}

recorrido posterior al pedido

recursividad

class Solution {
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        postorderTraversal(root,res);
        return res;
    }
    public void postorderTraversal(TreeNode root,ArrayList<Integer> list) {
    
    
        if(root!=null){
    
    
            postorderTraversal(root.left,list);
            postorderTraversal(root.right,list);
            list.add(root.val);
        }
        
    }
}

no recursivo

class Solution {
    
    
    public List<Integer> postorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> res=new ArrayList<>();
        Stack<TreeNode> stack=new Stack<>();
        TreeNode cur=root,next=null;
        while(cur!=null||!stack.isEmpty()){
    
    
            //先把结点给全进栈
            while(cur!=null){
    
    
                stack.push(cur);
                cur=cur.left;//左
            }
            //栈不为空
            if(!stack.isEmpty()){
    
    
                cur=stack.peek();
                if(cur.right==null||cur.right==next){
    
    
                    //判断栈顶结点的右子树是否为空,或者刚被访问过
                    cur=stack.pop();
                    res.add(cur.val);//根
                    next=cur;//记录刚访问
                    cur=null;//清空
                }else{
    
    
                    cur=cur.right;//右
                }
            }
        }


        return res;
    }

}

por fin

2023-8-7 16:00:45

Todos tenemos un futuro brillante

Les deseo todo lo mejor en sus exámenes de ingreso a posgrado, les deseo todo el éxito
en su trabajo, deseo que todos obtengan lo que desean, den me gusta, recopilen y sigan.


Supongo que te gusta

Origin blog.csdn.net/qq_51625007/article/details/132146961
Recomendado
Clasificación