二叉搜索树 全部题目
来源:互联网 发布:mac 更改用户文件夹 编辑:程序博客网 时间:2024/05/21 18:40
501. Find Mode in Binary Search Tree
Given a binary search tree (BST) with duplicates, find all the mode(s) (the most frequently occurred element) in the given BST.
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than or equal to the node's key.
- The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
- Both the left and right subtrees must also be binary search trees.
For example:
Given BST [1,null,2,2]
,
1 \ 2 / 2
return [2]
.
Note: If a tree has more than one mode, you can return them in any order.
Follow up: Could you do that without using any extra space? (Assume that the implicit stack space incurred due to recursion does not count).
Given a binary tree, determine if it is a valid binary search tree (BST).
Assume a BST is defined as follows:
- The left subtree of a node contains only nodes with keys less than the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Example 1:
2 / \ 1 3Binary tree
[2,1,3]
, return true.Example 2:
1 / \ 2 3
Binary tree [1,2,3]
, return false.
自己的解答,错误:
class Solution {public: bool isValidBST(TreeNode* root) { //错误,因为这个程序只保证根节点和子节点的关系,没有考虑根节点与子树的所有节点都需要满足要求 if(root==nullptr) return true; bool left=true,right=true; if(root->left) { if(root->val>root->left->val) left=isValidBST(root->left); else return false; } if(root->right) { if(root->val<root->right->val) { right=isValidBST(root->right); } else return false; } return left&&right; }};改正版本:
这个题目是中序遍历
538. Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.
Example:
Input: The root of a Binary Search Tree like this: 5 / \ 2 13Output: The root of a Greater Tree like this: 18 / \ 20 13530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes.
Example:
Input: 1 \ 3 / 2Output:1Explanation:The minimum absolute difference is 1, which is the difference between 2 and 1 (or between 2 and 3).
- 二叉搜索树 全部题目
- 题目36:二叉搜索树
- 题目1105: 二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 题目1009:二叉搜索树
- 使用maven构建ssm项目的pom文件
- 上周热点回顾(7.17-7.23)
- Windows10 Google protobuf 2.6.1 安装
- 437. Path Sum III
- 【复习笔记】手敲经常忘记的关于图的几个算法
- 二叉搜索树 全部题目
- 收集前端框架与链接
- RandomAccessFile类
- HTML之于浏览者交互
- How to Do Great Research-Managing Your Advisor
- 2017.08.11工作日记
- 打印乘法表(Java)
- Linux-视频监控系统(7)-播放器子系统2
- 帮未来岳父修电脑记