算法训练1-day17-二叉树

25 阅读2分钟

今天是二叉树

  1. 235. 二叉搜索树的最近公共祖先

因为二叉搜索树是有序的,每次遍历到一个节点时,我们可以比较rootpq的值,root的值大于pq,那就只用看root的左边,反之就只用看右边;当第一次遇到root的值在pq的中间时,这个root就是我们要找的节点,因为树是有序的,中序遍历的结果公共祖先一定在pq的中间

AC代码:

// 一般写法
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == NULL || root == p || root == q) return root;

        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);

        if (right != NULL && left != NULL) return root;
        return left != NULL ? left : right;
    }
};
// 利用二叉搜索树性质的写法
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if (root == NULL) return root;

        if (root->val > q->val && root->val > p->val)
            return lowestCommonAncestor(root->left, p, q);

        if (root->val < q->val && root->val < p->val)
            return lowestCommonAncestor(root->right, p, q);

        return root;
    }
};
  1. 701. 二叉搜索树中的插入操作

AC代码:

class Solution {
public:
    TreeNode* insertIntoBST(TreeNode* root, int val) {
        if(root == nullptr) return new TreeNode(val);

        if(root->val > val) root->left = insertIntoBST(root->left, val);
        else root->right = insertIntoBST(root->right, val);

        return root;
    }
};
  1. 450. 删除二叉搜索树中的节点

代码如下:

class Solution {
public:
    TreeNode* deleteNode(TreeNode* root, int key) {
        if (root == nullptr)
            return nullptr;

        if (root->val == key) {
            if (root->left == nullptr)
                return root->right;
            else if (root->right == nullptr)
                return root->left;
            // 左右孩子都非空,找到右子树的最小值
            TreeNode* prev = root;
            TreeNode* rightMax = root->right;
            while (rightMax->left != nullptr) {
                prev = rightMax;
                rightMax = rightMax->left;
            }
			
			// 特别地,当root的右节点没有左孩子时,
			// 我们只需要将它的右孩子的左子树设置为root的左子树即可
            if (prev == root) {
                rightMax->left = root->left;
            } else {
	            // 而如果root的右节点有左孩子
	            // 那么就要记录左孩子的前一个节点
	            // 即要注意这种情况:
	            //         5
	            //      3    9
	            //     2 4  7 10
	            //           8
	            // 删除5,此时,我们要将7移上来,并且7的右子树8要正确的移动到9的左子树上
	            // 然后再将7的左右子树设置为5的左右子树
	            // 而假如是删除3,那么我们要将4移动上来,
	            // 如果此时按照这套流程就会出错,
	            // 因为prev->left = rightMax->right会将3的左子树置为空
	            // 且rightMax->right = root->right会导致4的右子树指针指向自己
	            // 因此这种情况单独处理
                prev->left = rightMax->right;
                rightMax->left = root->left;
                rightMax->right = root->right;
            }

            delete root;
            return rightMax;
        } else if (root->val > key) {
            root->left = deleteNode(root->left, key);
        } else {
            root->right = deleteNode(root->right, key);
        }

        return root;
    }
};