输出二叉树的右视图&&岛屿数量&&二叉树的最大深度

124 阅读1分钟

NC136 输出二叉树的右视图

题目链接

1、解题思路

先利用二分的思想根据前序遍历和中序遍历构建一个树,然后采用树的层次遍历,但是层次遍历只取最后一个就好了。

2、代码
import java.util.*;


public class Solution {
   
    TreeNode helper(int[] xianxu, int[] zhongxu) {
        if (xianxu.length == 0) {
            return null;
        }
        TreeNode root = new TreeNode(xianxu[0]);
        if (xianxu.length == 1) {
            return root;
        }
        int index = -1;
        for (int i = 0; i < zhongxu.length; i++) {
            if (xianxu[0] == zhongxu[i]) {
                index = i;
                break;
            }
        }
        root.left = helper(Arrays.copyOfRange(xianxu, 1, index + 1), Arrays.copyOfRange(zhongxu, 0, index));
        root.right = helper(Arrays.copyOfRange(xianxu, index + 1, xianxu.length), Arrays.copyOfRange(zhongxu, index + 1, zhongxu.length));
        return root;

    }
	/**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 求二叉树的右视图
     * @param xianxu int整型一维数组 先序遍历
     * @param zhongxu int整型一维数组 中序遍历
     * @return int整型一维数组
     */
    public int[] solve(int[] xianxu, int[] zhongxu) {
        // write code here
        if (xianxu.length == 0) {
            return new int[0];
        }
        TreeNode root = helper(xianxu, zhongxu);
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
        while (!queue.isEmpty()) {
            ArrayList<Integer> list = new ArrayList<>();
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
                list.add(node.val);
            }
            lists.add(list);
        }
        int[] res = new int[lists.size()];
        for (int i = 0; i < res.length; i++) {
            ArrayList<Integer> list = lists.get(i);
            res[i] = list.get(list.size() - 1);
        }
        return res;
    }
}

NC109 岛屿数量

题目链接

1、解题思路

dfs的经典例题,寻找1的位置,然后根据这个位置去渲染其他相邻位置的1,只对原始的1计数。

2、代码
import java.util.*;


public class Solution {
    /**
     * 判断岛屿数量
     * @param grid char字符型二维数组 
     * @return int整型
     */
    private int[][] dir = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};

    private void dfs(int r, int c, char[][] grid) {
        if (r < 0 || r >= grid.length) {
            return;
        }
        if (c < 0 || c >= grid[0].length) {
            return;
        }
        if (grid[r][c] == '0') {
            return;
        }
        grid[r][c] = '0';
        for (int i = 0; i < 4; i++) {
            dfs(r + dir[i][0], c + dir[i][1], grid);
        }
    }

    public int solve(char[][] grid) {
        // write code here
        int res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == '1') {
                    res++;
                    dfs(i, j, grid);
                }
            }
        }
        return res;
    }
}

NC13 二叉树的最大深度

题目链接

1、解题思路

递归求解,当前节点的高度等于左子树和右子树最大高度加1。

2、代码
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    public int maxDepth (TreeNode root) {
        // write code here
        if (root == null) {
            return 0;
        }
        int leftH = maxDepth(root.left);
        int rightH = maxDepth(root.right);
        return leftH > rightH ? leftH + 1 : rightH + 1;
    }
}