Leetcode669.Trim a Binary Search Tree修建二叉树
给定一个二叉搜索树,同时给定最小边界L 和最大边界 R。通过修剪二叉搜索树,使得所有节点的值在[L, R]中 (R>=L) 。你可能需要改变树的根节点,所以结果应当返回修剪好的二叉搜索树的新的根节点。
错误的答案:
class Solution {
public:TreeNode* trimBST(TreeNode* root, int L, int R) {if(root == NULL)return NULL;if(root ->val < L){root = root ->right;trimBST(root, L, R);}else if(root ->val > R){root = root ->left;trimBST(root, L, R);}else{root ->left = trimBST(root ->left, L, R);root ->right = trimBST(root ->right, L, R);}return root;}
};
正确的答案:
class Solution {
public:TreeNode* trimBST(TreeNode* root, int L, int R) {if(root == NULL)return NULL;if(root ->val < L){root = trimBST(root ->right, L, R);}else if(root ->val > R){root = trimBST(root ->left, L, R);}else{root ->left = trimBST(root ->left, L, R);root ->right = trimBST(root ->right, L, R);}return root;}
};
转载于:https://www.cnblogs.com/lMonster81/p/10434033.html
Leetcode669.Trim a Binary Search Tree修建二叉树相关推荐
- LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)
669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...
- LeetCode 669. Trim a Binary Search Tree修剪二叉搜索树 (C++)
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
- LeetCode 669. Trim a Binary Search Tree
题目: Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so th ...
- LeetCode Binary Search Tree Iterator( 二叉树迭代器)
问题:给出一棵树,使用迭代器来输出. 思路:以中序遍历方式来输出.先中序遍历存入数组中,然后遍历.另一种方式是以模拟栈形式来中序遍历,先将左子树入栈,在遍历过程中,如果当前结点有右子树,需要将右子树按 ...
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- 【数组递归构造二叉树】LeetCode 108. Convert Sorted Array to Binary Search Tree
LeetCode 108. Convert Sorted Array to Binary Search Tree Solution1:我的答案 构造二叉树利用递归 /*** Definition fo ...
- 查找二叉树中出现次数最多的数 Find Mode in Binary Search Tree
为什么80%的码农都做不了架构师?>>> 问题: Given a binary search tree (BST) with duplicates, find all the ...
- LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List--转换二叉树为双向链表--Java,C++,Python解法
题目地址:Convert Binary Search Tree to Sorted Doubly Linked List - LeetCode Convert a BST to a sorted ci ...
- 【链表递归构造二叉树】LeetCode 109. Convert Sorted List to Binary Search Tree
LeetCode 109. Convert Sorted List to Binary Search Tree Solution1:我的答案 偷鸡摸狗的做法 /*** Definition for s ...
- 笔试算法题(58):二分查找树性能分析(Binary Search Tree Performance Analysis)
议题:二分查找树性能分析(Binary Search Tree Performance Analysis) 分析: 二叉搜索树(Binary Search Tree,BST)是一颗典型的二叉树,同时任 ...
最新文章
- python工程师薪资坑吗-6年Python开发工程师精心总结学习思路,再不看看就凉了...
- GraphSAGE: Inductive Representation Learning on Large Graphs
- 解决无法创建txt文档
- keepalived实现lvs高可用并负载均衡lamp
- VC中操作INI文件的API
- Controller上使用@CrossOrigin注解
- android程序获取WIFI的IP地址和MAC地址
- matlab水力学工具箱,新浪潮水工设计软件
- 加入阿里技术团队三年,哪些习惯让我在工作上持续受益?
- “互联网+”时代,漫谈影响用户体验的X因素
- 注意!SQLite被曝漏洞,Chrome 火狐等数千应用或受影响
- 搭建kafaka集群
- mysql 语句中last_Mysql函数:Last_insert_id()语法讲解
- VMware Explore 2022 China,赋能中国企业加速实现云智能
- 小虎电商浏览器:拼多多怎么看单品实时数据
- 如何使用poi解析word生成html目录结构
- 中国石油大学(北京)本科毕业论文答辩和论文选题PPT模板
- 搭建L2TP-***
- 有道笔记在Ubuntu上的安装
- Javascript的最大回文字符串
热门文章
- Telerik for AJAX RadGrid控件
- Eclipse开发工具组合键介绍
- Autodesk BIM 360将基础设施建模也推向云端
- FLASH+XML:构建简单易更新网站
- 让人深思:句法真的重要吗?邱锡鹏组提出一种基于Aspect的情感分析的强大基线...
- 【亚洲微软研究院】带你8篇论文梳理BERT相关模型进展与反思
- 【linux】常用命令之scp命令
- 盛大“传奇”的网游启示录
- SaaS架构设计之如何转化成SaaS多租户模式
- matplotlib绘图实例:pyplot、pylab模块及作图参数