day20

654. 最大二叉树

遍历数组,找到数组中最大的元素及其下标,新建一个节点,val为当前数组范围最大元素。以数组最大元素下标为节点分割左右数组,递归遍历

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
        public TreeNode constructMaximumBinaryTree(int[] nums) {
        return constructMaximumBinaryTree(nums, 0, nums.length);
    }
    public TreeNode constructMaximumBinaryTree(int[] nums, int begin, int end) {
        // 没有元素
        if (end - begin < 1) {
            return null;
        }
        // 只有一个元素
        if (end - begin == 1) {
            return new TreeNode(nums[begin]);
        }
        int maxIndex = begin;
        int maxValue = nums[begin];
        for (int i = begin + 1; i < end; i++) {
            if (nums[i] > maxValue) {
                maxIndex = i;
                maxValue = nums[i];
            }
        }
        TreeNode root = new TreeNode(maxValue);
        // 根据maxIndex划分左右子树
        root.left = constructMaximumBinaryTree(nums, begin, maxIndex);
        root.right = constructMaximumBinaryTree(nums, maxIndex + 1, end);
        return root;
    }
}

617. 合并二叉树

以root1为最终合并后的二叉树,前序遍历,root1节点为null返回root2节点,root2节点为null返回root1节点。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
       public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        root1.val += root2.val;
        root1.left = mergeTrees(root1.left, root2.left);
        root1.right = mergeTrees(root1.right, root2.right);
        return root1;
    }
}

700. 二叉搜索树中的搜索

利用二叉搜索树特性,若val大于root节点值则向右搜索,反之向左搜索

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
        public TreeNode searchBST(TreeNode root, int val) {
        if (root == null || root.val == val) {
            return root;
        }
        TreeNode result = null;
        if (root.val > val) {
            result = searchBST(root.left, val);
        }
        if (root.val < val) {
            result = searchBST(root.right, val);
        }
        return result;
    }
}

98. 验证二叉搜索树

二叉搜索树中序遍历后为一个递增的数据,注意不能简单判断当前节点大于左节点、当前节点大于右节点。利用中序遍历,判断当前节点大于前一个节点

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
     TreeNode max;
    public boolean isValidBST(TreeNode root) {
        if (root == null) {
            return true;
        }
        boolean left = isValidBST(root.left);
        if (!left) {
            return false;
        }
        if (max != null && root.val <= max.val) {
            return false;
        }
        max = root;
        boolean right = isValidBST(root.right);
        return right;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我来回答你的问题。首先,广度优先搜索(BFS)是一种图搜索算法,可以用来解决一些问题,比如迷宫问题。在迷宫问题中,我们需要找到一条从起点到终点的最短路径。下面是用 Python 实现 BFS 解决迷宫问题的示例代码: ```python from collections import deque # 定义一个二维数组表示迷宫,0 表示可以走,1 表示障碍物 maze = [ [0, 1, 0, 0, 0], [0, 1, 0, 1, 0], [0, 0, 0, 0, 0], [0, 1, 1, 1, 0], [0, 0, 0, 1, 0], ] # 定义起点和终点 start = (0, 0) end = (4, 4) # 定义四个方向,上下左右 directions = [(-1, 0), (1, 0), (0, -1), (0, 1)] def bfs(maze, start, end): # 定义队列,初始时将起点加入队列 queue = deque([start]) # 定义 visited 集合,用于记录已经访问过的位置 visited = set([start]) while queue: # 取出队列中的第一个节点 node = queue.popleft() # 如果当前节点是终点,返回到达终点的最短距离 if node == end: return distance[node[0]][node[1]] # 对于当前节点,遍历它的四个方向 for direction in directions: # 计算出下一个节点的坐标 next_node = (node[0] + direction[0], node[1] + direction[1]) # 如果下一个节点不越界且没有访问过,并且可以走(maze[next_node[0]][next_node[1]] == 0),将它加入队列和 visited 集合 if 0 <= next_node[0] < len(maze) and 0 <= next_node[1] < len(maze[0]) and next_node not in visited and maze[next_node[0]][next_node[1]] == 0: queue.append(next_node) visited.add(next_node) # 如果没有找到到达终点的路径,返回 -1 return -1 # 计算每个位置到起点的最短距离 distance = [[float('inf') for _ in range(len(maze[0]))] for _ in range(len(maze))] distance[start[0]][start[1]] = 0 bfs(maze, start, end) ``` 在上面的代码中,我们首先定义了一个迷宫,然后定义了起点和终点。接着,我们定义了四个方向,上下左右。接下来,我们定义了 bfs 函数,用于实现广度优先搜索。在 bfs 函数中,我们首先定义了一个队列和 visited 集合,用于记录已经访问过的位置。然后,我们将起点加入队列和 visited 集合。接着,我们进行循环,取出队列中的第一个节点,遍历它的四个方向。如果下一个节点不越界且没有访问过,并且可以走,我们将它加入队列和 visited 集合。最后,如果没有找到到达终点的路径,返回 -1。 最后,我们计算每个位置到起点的最短距离,并调用 bfs 函数求解最短路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值