Trim a Binary Search Tree

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 = 2

Output: 
    1
      \
       2

Example 2:

Input: 
    3
   / \
  0   4
   \
    2
   /
  1

  L = 1
  R = 3

Output: 
      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)
            return trimBST(root->right,L,R);
        else if(root->val>R)
            return trimBST(root->left,L,R);
        else
        {
            root->left=trimBST(root->left,L,R);
            root->right=trimBST(root->right,L,R);
            return root;
        }
    }
};
### 如何在 `a-tree-select` 中实现搜索高亮功能 为了实现在 `a-tree-select` 组件中输入框内的关键词匹配项能够被自动高亮显示的效果,可以利用自定义模板来处理树节点的渲染逻辑。下面是一个具体的解决方案: #### 自定义树节点渲染函数 通过设置 `tree-node-filter-prop` 属性指定用于过滤的关键属性名称,并结合 `slot-scope` 插槽作用域插槽来自定义展示方式。 ```html <a-tree-select style="width: 100%" :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }" placeholder="请选择..." allow-clear tree-default-expand-all :show-search="true" :filterTreeNode="filterOption" > <template slot=" treeNodeTitle" slot-scope="{ title, value, key, dataRef }"> <!-- 使用正则表达式替换并包裹 span 来标记关键字 --> <span v-html="highlightKeywords(title)"></span> </template> </a-tree-select> ``` 上述代码片段展示了如何配置 `a-tree-select` 的基本结构以及引入了一个名为 `highlightKeywords()` 方法用来处理字符串中的特定部分加上 HTML `<strong>` 或者其他标签以便浏览器解析成加粗样式[^1]。 #### 关键字高亮辅助方法 此 JavaScript 函数接收待处理的文字串作为参数,并返回经过修改后的带有内嵌 CSS 类的新字符串表示形式,从而达到视觉上的突出效果。 ```javascript methods: { highlightKeywords(text){ const reg = new RegExp(this.searchValue.trim(), "gi"); if (!this.searchValue || !reg.test(text)) return text; return text.replace(reg, match => `<strong>${match}</strong>`); }, filterOption(input, option){ // 如果没有输入值,则不执行筛选操作 if(!input){return true;} // 对比当前选项的内容是否包含用户所输入的关键字 return ( option.title.toLowerCase().indexOf(input.toLowerCase()) >= 0 || option.key === input ); } } ``` 这段脚本实现了两个主要的功能:一是将符合条件的部分文字转换为更醒目的外观;二是提供了一种简单的模糊查询机制以支持更加灵活的数据查找能力[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值