代码随想录二刷day22

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣235. 二叉搜索树的最近公共祖先
  • 二、力扣701. 二叉搜索树中的插入操作
  • 三、力扣450. 删除二叉搜索树中的节点


前言


一、力扣235. 二叉搜索树的最近公共祖先

递归

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if(root.val > p.val && root.val > q.val){
            return lowestCommonAncestor(root.left, p, q);
        }else if(root.val < p.val && root.val < q.val){
            return lowestCommonAncestor(root.right, p, q);
        }else{
            return root;
        }
    }
}

迭代

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

class Solution {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        TreeNode node = root;
        while(true){
            if(node.val > p.val && node.val > q.val){
                node = node.left;
            }else if(node.val < p.val && node.val < q.val){
                node = node.right;
            }else {
                break;
            }
        }
        return node;
    }
}

二、力扣701. 二叉搜索树中的插入操作

递归

/**
 * 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 insertIntoBST(TreeNode root, int val) {
        if(root == null){
            return new TreeNode(val);
        }
        fun(root, val);
        return root;
    }
    public void fun(TreeNode root, int val){
        if(root.val < val){
            if(root.right == null){
                root.right = new TreeNode(val);
                return;
            }else{
                fun(root.right, val);
            }
        }
        if(root.val > val){
            if(root.left == null){
                root.left = new TreeNode(val);
                return;
            }else{
                fun(root.left, 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 insertIntoBST(TreeNode root, int val) {
        if(root == null){
            return new TreeNode(val);
        }
        TreeNode pre = null, p = root;
        while(true){
            if(p.val > val){
                if(p.left == null){
                    p.left = new TreeNode(val);
                    break;
                }else{
                    p =p.left;
                }
            }
            if(p.val < val){
                if(p.right == null){
                    p.right = new TreeNode(val);
                    break;
                }else{
                    p = p.right;
                }
            }
        }
        return root;
    }
}

三、力扣450. 删除二叉搜索树中的节点

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

你可能感兴趣的:(算法,leetcode,职场和发展,数据结构,java)