二叉搜索树

二叉树设计

/**
 * 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;
 *     }
 * }
 */

1. 二叉搜索树中的搜索

题目描述

力扣链接

给定二叉搜索树(BST)的根节点 root 和一个整数值 val。

你需要在 BST 中找到节点值等于 val 的节点。 返回以该节点为根的子树。 如果节点不存在,则返回 null 。

示例 1:

输入:root = [4,2,7,1,3], val = 2
输出:[2,1,3]

代码

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

2. 验证二叉搜索树(双指针 + 中序遍历)

题目描述

力扣链接

给你一个二叉树的根节点 root ,判断其是否是一个有效的二叉搜索树。

有效 二叉搜索树定义如下:

  • 节点的左子树只包含 小于 当前节点的数。

  • 节点的右子树只包含 大于 当前节点的数。

  • 所有左子树和右子树自身必须也是二叉搜索树。

示例 1:

输入:root = [2,1,3]
输出:true

代码

//(双指针 + 中序遍历)
class Solution {
    public boolean isValidBST(TreeNode root) {
        if(root == null) return true;
        TreeNode pre = null; //定义前置指针
        Deque<TreeNode> stack = new ArrayDeque<>();
        TreeNode cur = root;
        while(cur != null || !stack.isEmpty()){
            if(cur != null){
                stack.push(cur);
                cur = cur.left;
            }else{
                cur = stack.pop();
                //vist
                if(pre != null && pre.val >= cur.val){    
                    return false;
                }
                pre = cur; 
                cur = cur.right;
            }
        }
        return true;
    }
}

//递归
class Solution {
    //记录前一个节点
    long val = Long.MIN_VALUE;
    boolean flag = true;
    public boolean isValidBST(TreeNode root) {
        if(root == null) return flag;
        isValidBST(root.left);
        if(root.val <= val){
            flag = false; 
        }
        val = root.val; 
        isValidBST(root.right);
        return flag;
    }
}

3. 删除二叉搜索树中的节点

题目描述

力扣链接

给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变。返回二叉搜索树(有可能被更新)的根节点的引用。

一般来说,删除节点可分为两个步骤:

首先找到需要删除的节点;
如果找到了,删除它。

示例 1:

输入:root = [5,3,6,2,4,null,7], key = 3
输出:[5,4,6,2,null,null,7]
解释:给定需要删除的节点值是 3,所以我们首先找到 3 这个节点,然后删除它。
一个正确的答案是 [5,4,6,2,null,null,7], 如下图所示。
另一个正确答案是 [5,2,6,null,4,null,7]

代码

class Solution {
    public TreeNode deleteNode(TreeNode root, int key) {
        root = delete(root,key);
        return root;
    }

    private TreeNode delete(TreeNode root, int key) {
        if (root == null) return null;

        if(root.val > key) root.left = delete(root.left, key);
        else if(root.val < key) root.right = delete(root.right, key);
        else{//删除逻辑
            if(root.left == null) return root.right; //左子树为空,直返回右子树
            if(root.right == null) return root.left; //有子树为空,直返回做子树
            //选取右子树的最左叶子结点作为删除结点的根节点
            TreeNode tmp = root.right;
            //找到最左叶子节点
            while (tmp.left != null) {
                tmp = tmp.left;
            }
            //将该节点赋给当前结点,其实是一种交换逻辑
            root.val = tmp.val;
            //然后将该叶子结点删除
            root.right = delete(root.right,tmp.val);
        }
        return root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值