【BST】700. Search in a Binary Search Tree; 701. Insert into a Binary Search Tree; 450. Delete Node in a BST;

Binary Search Tree

二叉搜索树问题。

遍历框架:

1 void BST(TreeNode root, int target) {
2     if (root.val == target)
3         // 找到目标,做点什么
4     if (root.val < target) 
5         BST(root.right, target);
6     if (root.val > target)
7         BST(root.left, target);
8 }

 

问题:

  • 700. Search in a Binary Search Tree 在二叉搜索树中,寻找指定节点。
  • 701. Insert into a Binary Search Tree 在二叉搜索树中,插入指定节点。
  • 450. Delete Node in a BST 在二叉搜索树中,删除指定节点。
 
解法:

700. Search in a Binary Search Tree

代码参考:
 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     TreeNode* searchBST(TreeNode* root, int val) {
15         if(!root) return nullptr;
16         if(val == root->val) return root;
17         else if(val < root->val) return searchBST(root->left, val);
18         else return searchBST(root->right, val);
19     }
20 };

 

701. Insert into a Binary Search Tree

代码参考:
 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     TreeNode* insertIntoBST(TreeNode* root, int val) {
15         if(!root) return new TreeNode(val);
16         else if(val<root->val) root->left = insertIntoBST(root->left, val);
17         else root->right = insertIntoBST(root->right, val);
18         return root;
19     }
20 };

 

450. Delete Node in a BST

删除节点中,需要分类讨论
删除的节点
  • 有0个子节点
    • 直接删除该节点,返回null
  • 有1个子节点child
    • 将子节点替代该节点,返回child
    •  
  • 有2个子节点
    • 找到 right 子树的最小值,Mright
    • 用Mright代替该节点(值替换)。返回该节点
    • 删除原来的Mright节点。
代码参考:
 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     //case 1: 0 child:
15     //        return null;
16     //case 2: 1 child:
17     //        return this child;
18     //case 3: 2 children:
19     //        find the min node Mright of right tree,
20     //        root->val = M->val
21     //        delete M -> case 1 above
22     TreeNode* deleteNode(TreeNode* root, int key) {
23         if(!root) return nullptr;
24         if(root->val == key) {
25             //case 1, case 2
26             if(!root->left) return root->right;
27             if(!root->right) return root->left;
28             //case 3
29             TreeNode* Mright = getMin(root->right);
30             root->val = Mright->val;
31             //recursionly delete Mright
32             root->right = deleteNode(root->right, Mright->val);
33         } else if(root->val > key) {
34             root->left = deleteNode(root->left, key);
35         } else {
36             root->right = deleteNode(root->right, key);
37         }
38         return root;
39     }
40     TreeNode* getMin(TreeNode* p) {
41         while(p->left){
42             p=p->left;
43         }
44         return p;
45     }
46 };

 

posted @ 2020-10-01 14:15  habibah_chang  阅读(145)  评论(0编辑  收藏  举报