题目考察的知识点是:
本题主要考察知识点是二叉树的遍历。
题目解答方法的文字分析:
先把二叉树左侧的数据得到,然后再去循环右侧的数据,最后得到所有数据。
本题解析所用的编程语言:
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 int[] bottomView (TreeNode root) {
// write code here
ArrayList<Integer> arrayList = new ArrayList<>();
bottom(root, arrayList);
int[] arr = new int[arrayList.size()];
int count = 0;
for (int i = 0; i < arrayList.size(); i++) {
arr[count++] = arrayList.get(i);
}
return arr;
}
public void bottom (TreeNode root, ArrayList<Integer> arrayList) {
if (root == null){
return;
}
if (root.left == null && root.right == null) {
arrayList.add(root.val);
return;
}
bottom(root.left, arrayList);
bottom(root.right, arrayList);
}
}

京公网安备 11010502036488号