题解 | #牛群仰视图#
牛群仰视图
https://www.nowcoder.com/practice/0f37a18320c4466abf3a65819592e8be
知识点:理解递归寻找的本质,dfs
思路:其实和找最大值没有区别,回忆我们之前写的找最大值,
拿到左子树和右子树,然后和当前的val进行对比,
这里其实就是判断,如果左子和右子树为null,就选中当前的val
也就是选取没有叶子节点的节点
编程语言:java
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 void find_leaf_node(TreeNode root, List<Integer> list){
if(root == null)
return;
if(root.left == null && root.right ==null)
list.add(root.val);
find_leaf_node(root.left,list);
find_leaf_node(root.right,list);
}
public int[] bottomView (TreeNode root) {
// write code here
List<Integer> list =new ArrayList<>();
find_leaf_node(root,list);
return list.stream().mapToInt(Integer::intValue).toArray();
}
}
