题解 | #牛群仰视图#

牛群仰视图

https://www.nowcoder.com/practice/0f37a18320c4466abf3a65819592e8be

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整型一维数组
     */
    public int[] bottomView (TreeNode root) {
        // write code here
         List<Integer> result = new ArrayList<>();
        if(root == null){
            return new int[]{};
        }
        preOrder(root,result);

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

    public void preOrder(TreeNode root,List<Integer> list){
        if(root == null){
            return;
        }

        if(root.left == null && root.right == null){
            list.add(root.val);
        }
        preOrder(root.left,list);
        preOrder(root.right,list);

    }
}

全部评论

相关推荐

10-14 21:00
门头沟学院 Java
吃花椒的狸猫:这个人说的倒是实话,特别是小公司,一个实习生哪里来的那么多要求
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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