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 static ArrayList<Integer> arrayList = new ArrayList<>();
public int[] bottomView (TreeNode root) {
// write code here
if(root==null){
return new int[]{};
}
search(root);
int[] arr = new int[arrayList.size()];
for(int i=0;i<arrayList.size();i++){
arr[i] = arrayList.get(i);
}
return arr;
}
public void search(TreeNode root){
if(root==null){
return;
}
if(root.left==null && root.right==null){
arrayList.add(root.val);
}
search(root.left);
search(root.right);
}
}
本题主要考察的是二叉树叶子节点的判断条件,所用编程为java.
我们首先需要确定题意,二叉树的仰视图指的是所有叶子结点按照从左往右的顺序的集合,只要明白这点此题并不难

京公网安备 11010502036488号