leetcode 669二叉搜索树

来源:互联网 发布:字体识别软件 编辑:程序博客网 时间:2024/06/11 02:27

Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that all its elements lies in [L, R] (R >= L). You might need to change the root of the tree, so the result should return the new root of the trimmed binary search tree.

Example 1:

Input:     1   / \  0   2  L = 1  R = 2Output:     1      \       2

Example 2:

Input:     3   / \  0   4   \    2   /  1  L = 1  R = 3Output:       3     /    2     / 1
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* trimBST(TreeNode* root, int L, int R) {
        if(root==NULL)
        {
            return NULL;
        }
        if(root->val>=L&&root->val<=R)
        {
            root->left=trimBST(root->left,L,R);
            root->right=trimBST(root->right,L,R);
            return root;
        }
        if(root->val<L)
            return trimBST(root->right,L,R);
        if(root->val>R)
            return trimBST(root->left,L,R);
    }
};

思路:

如果当前 root 正好在范围之内,那么把问题递归到它的左结点和右结点。
如果当前 root 不在范围内,比 L 小,那么 它和它的左子树 可以被抛弃了。
如果当前 root 不在范围内,比 R 大,那么 它和它的右子树 可以被抛弃了

首先明白什么叫二叉搜索树及其特点。

二叉搜索树的特点是,小的值在左边,大的值在右边,即

比如:

这样的结构有一个好处是很容易获得最大值(Maximum)、最小值(minimum)、某元素的前驱(Precursor)、某元素的后继(Successor)。

最大值:树的最右节点。

最小值:树的最左节点。

某元素前驱:左子树的最右。

某元素的后继:右子树的最左。

基本操作

二叉搜索树的基本操作包括searching、traversal、insertion以及deletion。

(代码为了省地方没有按照规范来写,真正写代码的时候请一定遵照规范)

① searching

复制代码
tree * search_tree(tree *l, item_type x){    if(l == null) return NULL;    if(l->item == x) return l;    if(x < l->item){        return (search_tree(l->left, x));    }       if(x > l->item){        return (search_tree(l->right, x));    }}
复制代码

时间复杂度为O(h),h为树的高度。

② traversal

由于小的节点在左边,大的节点在右边,因此使用中序(in-order)遍历可以方便的得到一个sorted list。

复制代码
void traverse_tree(tree *l){    if(l != NULL){        traverse_tree(l->left);        process_item(l->item);        traverse_tree(l->right);    }}
复制代码

时间复杂度为O(n),n为树的总结点数。

③ insertion

复制代码
insert_tree(tree **l, item_type x, tree *parent){    tree *p; /*temporary pointer*/    if(*l == NULL){        p = malloc(sizeof(tree));        p->item = x;        p->left = p->right = NULL;        p->parent = parent;        *l = p;        return;    }    if(x < (*l)->item){        insert_tree(&((*l)->left), x, *l);    }else{        insert_tree(&((*l)->right), x, *l);    }}
复制代码

时间复杂度为O(h),h为树的高度。

④deletion

在删除节点时有三种情况:

1)要删除的节点为叶节点

  那么直接删除即可。

2)要删除的节点有一个子节点

  那么删除掉该节点,并用其唯一的子节点代替自己的位置即可。

3)要删除的节点有两个子节点

  那么首先要找到该节点的右子树的最小值节点k,然后将该k替换掉待删除节点。

最坏情况下,时间复杂度为O(h)+指针的移动开销。

 

进阶

由上可知,二叉搜索树的dictionary operation(包括search、insertion、deletion)的时间复杂度均与O(h)相关,h为树的高度(log n),如果按照上述的insertion方法构建树,那么构建出来的树的形状各异,特别是当输入序列有序时,更会退化到链表的程度。所以,如果能用某种方法,将树的高度降低到最小,那么其dictionary operation的时间开销均可以降低,不过相对而言构建树的开销将增大。为了降低二叉搜索树的高度而提出了平衡二叉树(Balanced Binary Tree)的概念。它要求左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。这样就可以将搜索树的高度尽量减小。常用算法有红黑树、AVL、Treap、伸展树等。


原创粉丝点击