美文网首页
算法| 最大二叉树、合并二叉树、二叉搜索树中的搜索、验证二叉搜索

算法| 最大二叉树、合并二叉树、二叉搜索树中的搜索、验证二叉搜索

作者: 激扬飞雪 | 来源:发表于2022-12-04 18:21 被阅读0次

654. 最大二叉树

题目连接:https://leetcode.cn/problems/maximum-binary-tree/
思路:使用前序构建二叉树 区间[left, right)

/**
 * 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 {
    private TreeNode constructMaximumBinaryTree(int[] nums, int left, int right) {
        if (left >= right) return null;
        // if (right - left == 1) return new TreeNode(nums[left]);
        int maxIndex = left;
        for (int i = left + 1; i < right; i++) {
            if (nums[i] > nums[maxIndex]) {
                maxIndex = i;
            }
        }
        int val = nums[maxIndex];
        TreeNode treeNode = new TreeNode(val);
        treeNode.left = constructMaximumBinaryTree(nums, left, maxIndex);
        treeNode.right = constructMaximumBinaryTree(nums, maxIndex + 1, right);
        return treeNode;
    }
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        if (nums == null || nums.length == 0) return null;
        return constructMaximumBinaryTree(nums, 0, nums.length);
    }
}

二、 617. 合并二叉树

题目连接:https://leetcode.cn/problems/merge-two-binary-trees/
思路一:使用前序遍历两个二叉树,遇到root1 == null return root2;遇到root2 == null 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;
    }
}

思路二:使用一个stack, 依次存放root2 root1, root1.val += root2.val,最后返回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;
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root2);
        stack.push(root1);
        while (!stack.isEmpty()){
            TreeNode treeNode1 = stack.pop();
            TreeNode treeNode2 = stack.pop();
            treeNode1.val += treeNode2.val;
            if (treeNode1.right != null && treeNode2.right != null) {
                stack.push(treeNode2.right);
                stack.push(treeNode1.right);
            } else {
                if (treeNode1.right == null) {
                    treeNode1.right = treeNode2.right;
                }
            }
            if (treeNode1.left != null && treeNode2.left != null) {
                stack.push(treeNode2.left);
                stack.push(treeNode1.left);
            } else {
                if (treeNode1.left == null) {
                    treeNode1.left = treeNode2.left;
                }
            }
        }

        return root1;
    }
}

三、 700. 二叉搜索树中的搜索

题目连接:https://leetcode.cn/problems/search-in-a-binary-search-tree/
思路一:二叉搜索特性,左子树都比根节点小,右子数都比根节点大 使用递归法

/**
 * 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) return null;
        if (root.val < val) {
            return searchBST(root.right, val);
        } else if (root.val > val) {
            return searchBST(root.left, val);
        } else {
            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 searchBST(TreeNode root, int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < val) {
                cur = cur.right;
            } else if (cur.val > val) {
                cur = cur.left;
            } else {
                return cur;
            }
        }
        return null;
    }
}

四、 98. 验证二叉搜索树

题目连接:https://leetcode.cn/problems/validate-binary-search-tree/
思路一:使用中序 记录前一个节点,如果pre.val >= root.val 就返回false (使用递归中序方法)

/**
 * 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 {
    private TreeNode pre = null;
    public boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        boolean isLeft = isValidBST(root.left);
        if (!isLeft) return false;
        if (pre != null && pre.val >= root.val) return false;
        pre = root;
        return isValidBST(root.right);
    }
}

思路二:使用中序迭代法

/**
 * 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 boolean isValidBST(TreeNode root) {
        if (root == null) return true;
        TreeNode cur = root;
        TreeNode pre = null;
        Stack<TreeNode> stack = new Stack<>();

        while (cur != null || !stack.isEmpty()) {
            if (cur != null) {
                stack.push(cur);
                cur = cur.left;
            } else {
                TreeNode treeNode = stack.pop();
                System.out.println(treeNode.val);
                if (pre != null && pre.val >= treeNode.val) return false;
                pre = treeNode;
                cur = treeNode.right;
            }
        }
        return true;
    }
}

思路三:使用后序遍历

/**
 * 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 {
    private boolean isValidBST(TreeNode root, long min, long max) {
        if (root == null) return true;
        if (root.val <= min || root.val >= max) return false;
        boolean isLeft = isValidBST(root.left, min, root.val);
        if (!isLeft) return false;
        return isValidBST(root.right, root.val, max);
    }
    public boolean isValidBST(TreeNode root) {
        return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }
}

相关文章

  • 2019 算法面试相关(leetcode)--树、二叉树、二叉搜

    翻转二叉树二叉树的前序遍历二叉树的中序遍历二叉树的后序遍历验证二叉搜索树二叉树的最近公共祖先二叉搜索树的最近公共祖...

  • 二叉树数据结构及其算法操作(Java)

    二叉树的定义 向二叉树中插入节点 搜索二叉树中最大值和最小值 搜索二叉树的深度(height)和节点数(size)...

  • 二叉搜索树

    二叉搜索树 图解二叉树搜索算法图解:二叉搜索树算法二叉查找树(Binary Search Tree),(又:二叉搜...

  • 树,二叉树,搜索树

    树,二叉树,搜索树 资料 二叉搜索树 Demo 树的遍历 Demo 题目 ◎ 二叉树的中序遍历 ◎ 二叉树...

  • 2019-10-22

    最优二叉树搜索算法。

  • 二叉树

    查找二叉树中最大的搜索二叉树拓扑结构 96.不同的二叉搜索树给定n,求可以构成多少种二叉搜索树 95. 不同的二叉...

  • 二叉树基础

    二叉树的分类 完全二叉树与满二叉树 二叉搜索树BST 平衡二叉搜索树BBST因为二叉搜索树有可能退化为链表,降低查...

  • Swift 验证二叉搜索树- LeetCode

    题目: 验证二叉搜索树 验证二叉搜索树给定一个二叉树,判断其是否是一个有效的二叉搜索树。 假设一个二叉搜索树具有...

  • Swift实现搜索二叉树(BST)

    Swift实现搜索二叉树(BST) 二叉搜索树(BST)关于索索二叉树这里有详细的教程,下面我们主要针对二叉树的一...

  • LeetCode 有关二叉树的做题笔记 Python实现

    有关二叉树的做题笔记,Python实现 二叉树的定义 98. 验证二叉搜索树 Validate Binary Se...

网友评论

      本文标题:算法| 最大二叉树、合并二叉树、二叉搜索树中的搜索、验证二叉搜索

      本文链接:https://www.haomeiwen.com/subject/toyvfdtx.html