题解 | #二叉树的后序遍历#

二叉树的后序遍历

https://www.nowcoder.com/practice/1291064f4d5d4bdeaefbf0dd47d78541

import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 *   public TreeNode(int val) {
 *     this.val = val;
 *   }
 * }
 */

/**
 * NC192 二叉树的后序遍历
 * @author d3y1
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 程序入口
     *
     * @param root TreeNode类
     * @return int整型一维数组
     */
    public int[] postorderTraversal (TreeNode root) {
        // return solution1(root);
        return solution2(root);
    }

    /**
     * dfs
     * @param root
     * @return
     */
    private int[] solution1(TreeNode root){
        List<Integer> list = new ArrayList<>();

        postorder(list, root);

        int[] result = new int[list.size()];
        for(int i=0; i<list.size(); i++){
            result[i] = list.get(i);
        }

        return result;
    }

    /**
     * 递归
     * @param list
     * @param root
     */
    private void postorder(List<Integer> list, TreeNode root){
        // 空节点
        if(root == null){
            return;
        }

        // 左子树
        postorder(list, root.left);
        // 右子树
        postorder(list, root.right);
        // 根结点
        list.add(root.val);
    }

    /**
     * 非递归: 栈
     * @param root
     * @return
     */
    private int[] solution2(TreeNode root){
        List<Integer> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();

        TreeNode curr;
        // 上次访问
        TreeNode last = null;
        while(root!=null || !stack.isEmpty()){
            // 最左边
            while(root != null){
                stack.push(root);
                root = root.left;
            }
            
            curr = stack.pop();
            // 右节点为空或已访问
            if(curr.right==null || curr.right==last){
                // 访问当前节点
                list.add(curr.val);
                last = curr;
            }
            // 右节点非空且未访问
            else{
                // 当前节点再次入栈
                stack.push(curr);
                root = curr.right;
            }
        }
        
        int[] result = new int[list.size()];
        for(int i=0; i<list.size(); i++){
            result[i] = list.get(i);
        }
        
        return result;
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
06-14 19:09
门头沟学院 Java
darius_:给制造业搞的,什么物料管理生产管理,设备管理点检,最最关键的就是一堆报表看板。个人觉得没啥技术含量都是些基本的crud,但是业务很繁琐那种
点赞 评论 收藏
分享
码农索隆:有点耳熟,你们是我教过最差的一届
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务