代码随想录算法训练营day20| 654.最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树

Leetcode 654.最大二叉树

题目链接
思路:思路同上一天的根据前后中序、后序中序构造二叉树,都是找到最大的元素进行切割数组
代码

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return traversal(nums, 0, nums.length);
    }

    private TreeNode traversal(int[] nums, int leftIndex, int rightIndex) {
        // 终止条件
        if (leftIndex >= rightIndex) {
            return null;
        }
        // 只有一个元素的情况
        if (rightIndex - leftIndex == 1) {
            return new TreeNode(nums[leftIndex]);
        }
        // 最大值的下标
        int maxIndex = leftIndex;
        // 最大值
        int maxValue = nums[maxIndex];
        for (int i = leftIndex + 1; i < rightIndex; i++) {
            if (nums[i] > maxValue) {
                maxValue = nums[i];
                maxIndex = i;
            }
        }
        TreeNode root = new TreeNode(maxValue);

        // 根据maxIndex划分左右子树
        root.left = traversal(nums, leftIndex, maxIndex);
        root.right = traversal(nums, maxIndex + 1, rightIndex);
        return root;
    }
}

Leetcode 617.合并二叉树

题目链接

方法一:层序遍历

思路:层序遍历,层层处理
代码

//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) {
        // 层序遍历 迭代法 使用队列
        // 将root2加到root1上
        if (root1 == null) {
            return root2;
        }
        if (root2 == null) {
            return root1;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root1);
        queue.offer(root2);
        while (!queue.isEmpty()) {
            TreeNode node1 = queue.poll();
            TreeNode node2 = queue.poll();
            node1.val += node2.val;
            // 如果两棵树的左节点都不为空,入队
            if (node1.left != null && node2.left != null) {
                queue.offer(node1.left);
                queue.offer(node2.left);
            }
            // 如果两棵树的右节点都不为空,入队
            if (node1.right != null && node2.right != null) {
                queue.offer(node1.right);
                queue.offer(node2.right);
            }
            // 若node1的左节点为空,直接赋值
            if (node1.left == null && node2.left != null) {
                node1.left = node2.left;
            }
            // 若node1的右节点为空,直接赋值
            if (node1.right == null && node2.right != null) {
                node1.right = node2.right;
            }
        }
        return 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;
    }
}

Leetcode 700.二叉搜索树中的搜索

题目链接

方法一:递归法

思路:根据搜索二叉树的特性就行递归
代码

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        // 递归法
        if (root == null || root.val == val) {
            return root;
        }
        TreeNode result = null;
        if (val < root.val) {
            result = searchBST(root.left, val);
        }
        if (val > root.val) {
            result = searchBST(root.right, val);
        }
        return result;
    }
}

方法二:迭代法

思路
代码

class Solution {
    public TreeNode searchBST(TreeNode root, int val) {
        // 迭代法
        while (root != null) {
            if (val < root.val) {
                root = root.left;
            } else if (val > root.val) {
                root = root.right;
            } else {
                return root;
            }
        }
        return null;
    }
}

Leetcode 98.验证二叉搜索树

题目链接

方法一:递归法

思路:二叉搜索的顺序就是中序遍历的顺序,根据双指针法比较前后两个元素的值进行判断
代码

class Solution {
        // 双指针,用来记录前一个节点的值
    TreeNode pre;
    public boolean isValidBST(TreeNode root) {
        // 递归法 中序遍历即为二叉搜索树的顺序
        if (root == null) {
            return true;
        }
        // 左
        boolean left = isValidBST(root.left);
        // 中
        if (pre != null && pre.val >= root.val) {
            return false;
        }
        pre = root;
        // 右
        boolean right = isValidBST(root.right);
        return left && right;   
    }
}

方法二:迭代法

思路:中序遍历,比较前后两个元素
代码

class Solution {
    public boolean isValidBST(TreeNode root) {
        // 迭代法
        if (root == null) {
            return true;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        while (root != null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                // 左
                root = root.left;
            }
            // 中
            TreeNode node = stack.pop();
            if (pre != null && node.val <= pre.val) {
                return false;
            }
            pre = node;
            // 右
            root = node.right;
        }
        return true;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值