mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 07:35:35 +08:00
Update 0669.修剪二叉搜索树.md
添加代码高亮
This commit is contained in:
@ -79,7 +79,7 @@ public:
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
TreeNode* trimBST(TreeNode* root, int low, int high)
|
||||
```
|
||||
|
||||
@ -87,7 +87,7 @@ TreeNode* trimBST(TreeNode* root, int low, int high)
|
||||
|
||||
修剪的操作并不是在终止条件上进行的,所以就是遇到空节点返回就可以了。
|
||||
|
||||
```
|
||||
```cpp
|
||||
if (root == nullptr ) return nullptr;
|
||||
```
|
||||
|
||||
@ -97,7 +97,7 @@ if (root == nullptr ) return nullptr;
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
if (root->val < low) {
|
||||
TreeNode* right = trimBST(root->right, low, high); // 寻找符合区间[low, high]的节点
|
||||
return right;
|
||||
@ -108,7 +108,7 @@ if (root->val < low) {
|
||||
|
||||
代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
if (root->val > high) {
|
||||
TreeNode* left = trimBST(root->left, low, high); // 寻找符合区间[low, high]的节点
|
||||
return left;
|
||||
@ -119,7 +119,7 @@ if (root->val > high) {
|
||||
|
||||
最后返回root节点,代码如下:
|
||||
|
||||
```
|
||||
```cpp
|
||||
root->left = trimBST(root->left, low, high); // root->left接入符合条件的左孩子
|
||||
root->right = trimBST(root->right, low, high); // root->right接入符合条件的右孩子
|
||||
return root;
|
||||
@ -133,7 +133,7 @@ return root;
|
||||
|
||||
如下代码相当于把节点0的右孩子(节点2)返回给上一层,
|
||||
|
||||
```
|
||||
```cpp
|
||||
if (root->val < low) {
|
||||
TreeNode* right = trimBST(root->right, low, high); // 寻找符合区间[low, high]的节点
|
||||
return right;
|
||||
@ -142,7 +142,7 @@ if (root->val < low) {
|
||||
|
||||
然后如下代码相当于用节点3的左孩子 把下一层返回的 节点0的右孩子(节点2) 接住。
|
||||
|
||||
```
|
||||
``` cpp
|
||||
root->left = trimBST(root->left, low, high);
|
||||
```
|
||||
|
||||
|
Reference in New Issue
Block a user