-
## 时间复杂度训练II(树形结构分析时间复杂度:root为操作O(n) or O(1), 子树为T(n/2))
- **二叉树的节点数为n,那么深度为?**
- 需要考虑非完全二叉树的情况,即近似“链表“的情况,最坏的深度为n而非logn
- 这个思考涉及到Recursion,即递归算法在Tree中的实现时,需要谨慎思考
- 如果深度过大,利用递归思想则有可能造成…
-
Hello, I'm a GSSoC'21 participant. I would like to implement Searching operation in Binary Search Tree along with proper function descriptions in C++. Kindly assign this task to me.
-
实现一个二叉搜索树迭代器。你将使用二叉搜索树的根节点初始化迭代器。
调用 next() 将返回二叉搜索树中的下一个最小的数。
BSTIterator iterator = new BSTIterator(root);
iterator.next(); // 返回 3
iterator.next(); // 返回 7
iterator.hasNext(); // 返回 …
-
stack的最顶部就是整个BST的最小值(最左侧的叶子节点),每次pop一个值,都要把该root的right加进去,要熟悉BST的性质,左侧的都比右侧的小
![image](https://cloud.githubusercontent.com/assets/18609174/20287692/e8323fcc-aa82-11e6-95f1-41c8cb93e4b8.png)
详见230题
…
-
递归以后,如果去的是root.left, 之前的节点root.val变成了最大值,同理root.right,root.val变成最小值比较
/**
- Definition for a binary tree node.
- public class TreeNode {
- int val;
- TreeNode left;
- TreeNode right;
- …
-
## [99. Recover Binary Search Tree](https://leetcode.com/problems/recover-binary-search-tree/)
二叉搜索树中的两个节点被错误地交换。
请在不改变其结构的情况下,恢复这棵树。
#### Example 1
```text
Input: [1,3,null,null,2]
…
-
## [96. Unique Binary Search Trees](https://leetcode.com/problems/unique-binary-search-trees/)
给定一个整数 `n`,求以 `1 ... n` 为节点组成的二叉搜索树有多少种?
#### Example
```text
Input: 3
Output: 5
Explanation:…
-
## [98. Validate Binary Search Tree](https://leetcode.com/problems/validate-binary-search-tree/)
给定一个二叉树,判断其是否是一个有效的二叉搜索树。
假设一个二叉搜索树具有如下特征:
- 节点的左子树只包含小于当前节点的数。
- 节点的右子树只包含大于当前节点的数。
- 所有左子树…
-
![image](https://user-images.githubusercontent.com/32625059/66127030-6554b100-e61d-11e9-965b-46869726ba24.png)
-
![image](https://user-images.githubusercontent.com/32625059/66123141-edce5400-e613-11e9-8cea-b5772ca4b8b2.png)