题解 | #牛群仰视图#

牛群仰视图

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){
        if (root==null){
            return new int[]{};
        }
        ArrayList<Integer> list = new ArrayList<>();
        dfs(root,list);
        int[] dp = new int[list.size()];
        for (int i=0;i<list.size();i++){
            dp[i]=list.get(i);
        }
        return dp;
    }
    public void dfs(TreeNode root, ArrayList<Integer> list){
        if (root==null){
            return;
        }
        if (root.left==null&&root.right==null){
            list.add(root.val);
            return;
        }
        dfs(root.left,list);
        dfs(root.right,list);
    }
}

#实习,投递多份简历没人回复怎么办#
全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

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