94. Recorrido en orden del árbol binario (implementación de Java) --LeetCode

tema:

Dada la raíz del nodo raíz de un árbol binario, devuelve su recorrido de orden medio.

Ejemplo 1:
Inserte la descripción de la imagen aquí

输入:root = [1,null,2,3]
输出:[1,3,2]

Ejemplo 2:

输入:root = []
输出:[]

Ejemplo 3:

输入:root = [1]
输出:[1]

Ejemplo 4:
Inserte la descripción de la imagen aquí

输入:root = [1,2]
输出:[2,1]

Ejemplo 5:

输入:root = [1,null,2]
输出:[1,2]

rápido:

  • El número de nodos en el árbol está en el rango [0, 100]
  • -100 <= Node.val <= 100

Avanzado: el algoritmo recursivo es muy simple, ¿puedes hacerlo mediante un algoritmo iterativo?

Solución 1: recursividad

    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> result = new ArrayList<>();
        recursive(root,result);
        return result;
    }

    private void recursive(TreeNode root, ArrayList<Integer> result) {
    
    
        if (root==null)return;
        recursive(root.left,result);
        result.add(root.val);
        recursive(root.right,result);
    }

Complejidad de tiempo: activado

Complejidad espacial: activada
Inserte la descripción de la imagen aquí

Solución 2: apilar

/**
 * 思路:
 * 移动当前节点,入栈,直到走到最左。
 * 最左的值加入节点到结果集,之后继续移动当前节点走右边节点
 */
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> result = new ArrayList<>();
        ArrayDeque<TreeNode> stack = new ArrayDeque<>();
        while (root!=null||!stack.isEmpty()){
    
    
            while (root!=null){
    
    
                stack.push(root);
                root=root.left;
            }
            TreeNode pop = stack.pop();
            result.add(pop.val);
            root=pop.right;
        }
        return result;
    }

Complejidad de tiempo: activado

Complejidad espacial: activada
Inserte la descripción de la imagen aquí

Solución 3: Morris

    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> result = new ArrayList<>();
        while (root != null) {
    
    
            if (root.left != null) {
    
    
                TreeNode tail = root.left;
                while (tail.right != null && tail.right != root) tail = tail.right;
                if (tail.right==null){
    
    
                    tail.right=root;
                    root=root.left;
                }else {
    
    
                	//根据链表往回找的时候把值加入result
                    result.add(root.val);
                    root=root.right;
                }
            } else {
    
    
                result.add(root.val);
                root = root.right;
            }
        }
        return result;
    }

Complejidad de tiempo: activado

Complejidad espacial: activada
Inserte la descripción de la imagen aquí

Solución 4: marca de color

/**
 * 思路:
 * 颜色标记,0是没走过,1是走过
 * 在栈中存放paris 0/1,node
 * 如果当前节点是没走过,按照右根左的顺序加入栈中
 */
    public List<Integer> inorderTraversal(TreeNode root) {
    
    
        ArrayList<Integer> result = new ArrayList<>();
        if (root==null)return result;
        ArrayDeque<Pair<Integer,TreeNode>> stack = new ArrayDeque<>();
        stack.push(new Pair<>(0,root));
        while (!stack.isEmpty()){
    
    
            Pair<Integer, TreeNode> pop = stack.pop();
            if (pop.getKey()==0) {
    
    
                if (pop.getValue().right != null) stack.push(new Pair<>(0, pop.getValue().right));
                stack.push(new Pair<>(1, pop.getValue()));
                if (pop.getValue().left != null) stack.push(new Pair<>(0, pop.getValue().left));
            }else {
    
    
                result.add(pop.getValue().val);
            }
        }
        return result;
    }

Complejidad de tiempo: activado

Complejidad espacial: activada
Inserte la descripción de la imagen aquí

Supongo que te gusta

Origin blog.csdn.net/qq_38783664/article/details/111773856
Recomendado
Clasificación