LeetCode-95-不同的二叉搜索树II

Posted wangsong412

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode-95-不同的二叉搜索树II相关的知识,希望对你有一定的参考价值。

二叉搜索数的插入、查找、删除

 

public class TestUtil {
    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode();
        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        for (Integer value : list) {
            insert(treeNode, value);
        }

    }

    private static void insert(TreeNode root, Integer value) {
        if (root == null) {
            TreeNode treeNode = new TreeNode();
            treeNode.value = value;
            treeNode.lChild = null;
            treeNode.rChild = null;

            root = treeNode;
        }

        if (root.value > value) {
            insert(root.lChild, value);
        }

        if (root.value < value) {
            insert(root.rChild, value);
        }
    }

    private static TreeNode find(TreeNode root, Integer value) {
        if (root == null || root.value.equals(value)) {
            return root;
        }

        if (root.value > value) {
            return find(root.lChild, value);
        }

        if (root.value < value) {
            return find(root.rChild, value);
        }

        return null;
    }

    private static TreeNode delete(TreeNode root, Integer value) {
        if (root == null) {
            return root;
        }

        TreeNode father = null;
        TreeNode delete = root;
        while(delete != null) {
            if (delete.value.equals(value)) {
                break;
            } if (delete.value > value) {
                father = delete;
                delete = delete.lChild;
            } else {
                father = delete;
                delete = delete.rChild;
            }
        }

        if (delete.lChild != null) {
            if (delete.lChild.rChild == null) {
                delete.value = delete.lChild.value;
                delete.lChild = delete.lChild.lChild;
            } else {
                TreeNode preNode = delete.lChild;
                TreeNode tempNode = delete.lChild.rChild;
                while (tempNode.rChild != null) {
                    preNode = tempNode;
                    tempNode = tempNode.rChild;
                }

                delete.value = tempNode.value;
                preNode.rChild = tempNode.lChild;
            }
            return root;
        }

        if (delete.rChild != null) {
            if (delete.rChild.lChild == null) {
                delete.value = delete.rChild.value;
                delete.rChild = delete.rChild.rChild;
            } else {
                TreeNode preNode = delete.rChild;
                TreeNode tempNode = delete.rChild.lChild;
                while (tempNode.rChild != null) {
                    preNode = tempNode;
                    tempNode = tempNode.rChild;
                }

                delete.value = tempNode.value;
                preNode.rChild = tempNode.lChild;
            }
            return root;
        }

        if (father == null) {
            root = null;
        } else if (father.lChild.value == delete.value) {
            father.lChild = null;
        } else {
            father.rChild = null;
        }

        return root;
    }


}

class TreeNode {
    Integer value;
    TreeNode lChild;
    TreeNode rChild;
}

  

二叉树搜索树(即:二叉排序树,二叉查找树)
二叉排序树或者是一颗空树,或者是具有下列性质的二叉树:
(1)若它的左子树不空,则左子树上的所有结点的值均小于它的根结点的值。
(2)若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值。
(3)它的左、右子树叶分别是二叉排序树。


二叉排序树是递归定义的。由定义可得:中叙遍历二叉排序树是可以得到一个结点值递增的序列。

 

参考: https://blog.csdn.net/weixin_41423494/article/details/89145139

以上是关于LeetCode-95-不同的二叉搜索树II的主要内容,如果未能解决你的问题,请参考以下文章

Leetcode 95.不同的二叉搜索树II

Leetcode 95.不同的二叉搜索树II

LeetCode(95): 不同的二叉搜索树 II

leetcode 95. 不同的二叉搜索树II

LeetCode95. 不同的二叉搜索树 II

LeetCode-95-不同的二叉搜索树II