【QZMS】3 BST LCA depth树

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>>res;

    void dfs(TreeNode* root, queue<TreeNode*>q, vector<int>path) {
        if(!root) return;
        if(root->left) q.push(root->left);
        if(root->right) q.push(root->right);
        if(q.empty()) {
            res.push_back(path);
            return;
        }
        int n = q.size();
        while(n --) {
            TreeNode* cur = q.front(); q.pop();
            path.push_back(cur->val);
            dfs(cur, q, path);
            // 回复现场
            q.push(cur);
            path.pop_back();
        }
    }

    vector<vector<int>> BSTSequences(TreeNode* root) {
        if(root == NULL) return {{}};
        queue<TreeNode*>q;
        vector<int>path;
        path.push_back(root->val);
        dfs(root, q, path);
        return res;
    }
};

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
        if(!root || root == p || root == q) return root;
        TreeNode* left = lowestCommonAncestor(root->left, p, q);
        TreeNode* right = lowestCommonAncestor(root->right, p, q);
        if(left == NULL) return right;
        if(right == NULL) return left;
        return root;
    }
};

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
        if(!root || !p) return NULL;
        if(p->val >= root->val) {
            return inorderSuccessor(root->right, p);
        } else {
            TreeNode* left = inorderSuccessor(root->left, p);
            return left ? left : root;
        }
    }
};

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isValidBST(TreeNode* root) {
        if(root == NULL) return true;
        TreeNode*maxLeft = root->left, *minRight = root->right;
        while(maxLeft != NULL && maxLeft->right != NULL) {
            maxLeft = maxLeft->right;
        }
        while(minRight != NULL && minRight->left != NULL) {
            minRight = minRight->left;
        }
        bool res = (maxLeft == NULL || maxLeft->val < root->val) && (minRight == NULL || minRight->val > root->val);
        return res && isValidBST(root->left) && isValidBST(root->right);
    }
};

在这里插入图片描述

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    int depth(TreeNode* root) {
        if(root == NULL) return 0;
        int left = depth(root->left);
        int right = depth(root->right);
        return max(left, right) + 1;
    }

    bool isBalanced(TreeNode* root) {
        if(!root) return true;

        if(abs(depth(root->left) - depth(root->right)) < 2) {
            return isBalanced(root->left) && isBalanced(root->right);
        }
        return false;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值