算法修炼Day20|● 654.最大二叉树 ● 617.合并二叉树 ● 700.二叉搜索树中的搜索 ● 98.验证二叉搜索树

95 阅读3分钟

LeetCode:654. 最大二叉树 - 力扣(LeetCode)

1.思路

遍历数组,递归内容选出数组中最大值和对应索引作为根节点,将数组分为左右子数组分别作为根节点的左右子树,对两个子数组采用同样的操作方式,向左向右进行递归遍历,返回根节点即可。 递归终止条件为当前数组为空或者当前数组只有一个元素直接加入下一个节点即可。

2.代码实现
// 逻辑是清晰的,细节蛮多的
class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return constructTree(nums, 0, nums.length);
        
    }

    public TreeNode constructTree(int[] nums, int leftIndex, int rightIndex) {
        // 数组中没有元素时
        if (rightIndex - leftIndex < 1) {
            return null;
        }
        // 数组中有元素时
        if (rightIndex - leftIndex == 1) {
            return new TreeNode(nums[leftIndex]);
        }
        // 获取当前数组中的最大值和对应的索引
        int maxIndex = leftIndex;
        int maxValue = nums[leftIndex];
        for (int i = leftIndex + 1; i < rightIndex; i++) {
            if (nums[i] > maxValue) {
                maxValue = nums[i];
                maxIndex = i;
            }
        }

        TreeNode root = new TreeNode(maxValue);
        // 根据当前层最大值划分左右子树
        root.left = constructTree(nums, leftIndex, maxIndex);
        root.right = constructTree(nums, maxIndex + 1, rightIndex);
        return 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 constructMaximumBinaryTree(int[] nums) {
        if (nums.length == 0 || nums == null) {
            return null;
        }
        Map<Integer, Integer> map = new HashMap<>();
        // 找出最大数值和对应的索引
        int maxValue = -1;
        int index = -1;
        for (int i = 0; i < nums.length; i++) {
            maxValue = Math.max(maxValue, nums[i]);
            if (nums[i] == maxValue) {
                index = i;
            }
        }
        // 获取索引
        map.put(maxValue, index);
        index = map.get(maxValue);
        
        TreeNode head = new TreeNode(maxValue);
        for (int i = 0; i < nums.length; i++) {
            TreeNode temp = new TreeNode(nums[i]);
            if (i < index) {
                head.left = constructMaximumBinaryTree(Arrays.copyOfRange(nums, 0, index));
            } else if (i > index) {
                head.right = constructMaximumBinaryTree(Arrays.copyOfRange(nums, index + 1, nums.length));
            } else {
                continue;
            }
        }
        return head;
    }
}

3.复杂度分析

时间复杂度:O(n).

空间复杂度:O(n).

LeetCode:617. 合并二叉树 - 力扣(LeetCode)

1.思路

遍历顺序:前序(便于理解) 终止条件:左右子树均为空时结束

2.代码实现
// 在原树上叠加
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;
    }
}

// 新定义节点
class Solution {
    public TreeNode mergeTrees(TreeNode root1, TreeNode root2) {
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        // 中
        TreeNode node = new TreeNode();
        node.val = root1.val + root2.val;
        // 向左遍历
        node.left = mergeTrees(root1.left, root2.left);
        // 向右遍历
        node.right = mergeTrees(root1.right, root2.right);

        return node;
    }
}
3.复杂度分析

时间复杂度:O(n).

空间复杂度:O(n).调用递归方法的空间开销在栈的层数,层数取决于二叉树的高度,n个节点的二叉树最大高度/深度为n.

LeetCode:700. 二叉搜索树中的搜索 - 力扣(LeetCode)

1.思路

前序遍历:中左右;

终止条件:节点为空或者当前节点值 root.val == val;

返回操作有点迷惑。

2.代码实现
/**
 * 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;
        } 
        if (root.val > val) {
            return searchBST(root.left, val);
        } else {
            return searchBST(root.right, val);
        }
    }
}
3.复杂度分析

时间复杂度:O(n). 空间复杂度:O(n).

LeetCode:98. 验证二叉搜索树 - 力扣(LeetCode)

1.思路

中序遍历,在遍历过程中比较大小

2.代码实现
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;
    }
}
3.复杂度分析

时间复杂度:O(n).

空间复杂度:O(n).