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

二叉树的后序遍历

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;
 *   }
 * }
 */

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param root TreeNode类 
     * @return int整型一维数组
     */
    private void recursivelyPostorder(TreeNode vroot, List<Integer> temp){
        if(vroot.left!=null) recursivelyPostorder(vroot.left, temp);
        if(vroot.right !=null) recursivelyPostorder(vroot.right, temp);

        temp.add(vroot.val);
    }

    
    public int[] postorderTraversal (TreeNode root) {
        // write code here

        if(root==null){
            int[] treeList = new int[0];
            return treeList;
        }
        List<Integer> temp = new ArrayList();
        recursivelyPostorder(root, temp);

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

        return treeList;
    }
}

全部评论

相关推荐

白火同学:先说结论,对于一份实习简历来说,整体还是挺不错的,技术深度和广度都到位,找到一份中小厂的实习没啥问题。 再说说能优化的点吧。 1、量化结果,项目中很多工作量化一下结果给面试官的感受会更直观一些,也能体现你对应用该项技术的理解(在众多技术为什么要用它,运行性能或者说开发效率往往是一大考虑指标;而不是说大家做这种功能都用它,所以我用它)。 2、突出亮点,项目中可以从“工作职责”择一些“个人亮点”另写一块,优先去写开发过程中遇到的xx问题,使用xx技术达到xx效果,针对性去写一些疑杂难的功能,能带出你个人思考和解决的过程。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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