算法训练(leetcode)第十五天 | 654. 最大二叉树、617. 合并二叉树、700. 二叉搜索树中的搜索、98. 验证二叉搜索树

刷题记录

654. 最大二叉树

leetcode题目地址

本题和106. 从中序与后序遍历序列构造二叉树思路类似,递归建树,每次找出最大值赋值给当前节点,接着递归左子树和右子树。

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

cpp 复制代码
// c++
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* createTree(vector<int>& nums, int left, int right){
        if(left>right) return NULL;
        // 记录最大值下标
        int max_idx = left;
        int idx = left;
        // 找最大
        for(; idx<=right; idx++){
            if(nums[idx] > nums[max_idx]) {
                max_idx = idx;
            }
        }
        // 用最大值创建节点
        TreeNode* root = new TreeNode(nums[max_idx]);
        // 递归左右子树
        root->left = createTree(nums, left, max_idx-1);
        root->right = createTree(nums, max_idx+1, right);
        return root;
    }
    
    TreeNode* constructMaximumBinaryTree(vector<int>& nums) {
        TreeNode* root = createTree(nums, 0, nums.size()-1);
        return root;
    }
};

617. 合并二叉树

leetcode题目地址

上题思路类似,只不过是同时遍历两个树,题目新颖,第一次遇到。

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

cpp 复制代码
// c++
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* Order(TreeNode* root1, TreeNode* root2){
        if(!root1 && !root2) return nullptr;
        TreeNode* root;
        if(root1 && root2){
            root = new TreeNode(root1->val + root2->val);
            root->left = Order(root1->left, root2->left);
            root->right = Order(root1->right, root2->right);
            
        }else if(!root1){
            root = new TreeNode(root2->val);
            root->left = Order(nullptr, root2->left);
            root->right = Order(nullptr, root2->right);
        }else{
            root = new TreeNode(root1->val);
            root->left = Order(root1->left, nullptr);
            root->right = Order(root1->right, nullptr);
        }
        return root;
    }
    TreeNode* mergeTrees(TreeNode* root1, TreeNode* root2) {
        // TreeNode root;
        return Order(root1, root2);
    }
};

700. 二叉搜索树中的搜索

leetcode题目地址

借助前序遍历来查找,找到则返回该节点。

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

直接前序遍历

cpp 复制代码
// c++
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void preOrder(TreeNode* root, int val, TreeNode* &result){
        if(!root) return;
        if(root->val == val){
            result = root;
            return;
        }
        // 没找到时递归左右子树,找到了就停止
        if(!result)
            preOrder(root->left, val, result);
        if(!result)
            preOrder(root->right, val, result);
        
    }
    TreeNode* searchBST(TreeNode* root, int val) {
        TreeNode* result = nullptr;
        preOrder(root, val, result);
        return result;
    }
};

借助BST性质

借助BST左小右大的性质来提高查找效率。(其实就是改一个递归左右子树的判断)

cpp 复制代码
// c++
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void preOrder(TreeNode* root, int val, TreeNode* &result){
        if(!root) return;
        if(root->val == val){
            result = root;
            return;
        }
        // 没找到时递归左右子树,找到了就停止
        if(root->val > val)
            preOrder(root->left, val, result);
        if(root->val < val)
            preOrder(root->right, val, result);
        
    }
    TreeNode* searchBST(TreeNode* root, int val) {
        TreeNode* result = nullptr;
        preOrder(root, val, result);
        return result;
    }
};

98. 验证二叉搜索树

leetcode题目地址

与上题类似,这里使用中序遍历。BST的中序遍历结果是一个单调递增序列,因此在遍历时出现递减(或相等)则不是BST。

时间复杂度: O ( n ) O(n) O(n)
空间复杂度: O ( n ) O(n) O(n)

中规中矩版

cpp 复制代码
// c++
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void inOrder(TreeNode* root, bool & result, TreeNode* &last){
        if(!root) return ;
        inOrder(root->left, result, last);
        if(last && last->val >= root->val) {
            result = false;
            return;
        }
        last = root;
        inOrder(root->right, result, last);
    }
    bool isValidBST(TreeNode* root) {
        bool result = true;
        TreeNode* last = nullptr;
        inOrder(root, result, last);
        return result;
    }
};

简洁版:

简化了一下参数传递,改成函数值返回。

cpp 复制代码
// c++
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool inOrder(TreeNode* root,  TreeNode* &last){
        if(!root) return true;
        bool left = inOrder(root->left, last);
        if(last && last->val >= root->val) {
            return false;
        }
        last = root;
        bool right = inOrder(root->right, last);
        return left && right;
    }
    bool isValidBST(TreeNode* root) {
        TreeNode* last = nullptr;
        return inOrder(root, last);
    }
};
相关推荐
草莓熊Lotso19 分钟前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
KyollBM25 分钟前
【CF】Day75——CF (Div. 2) B (数学 + 贪心) + CF 882 (Div. 2) C (01Trie | 区间最大异或和)
c语言·c++·算法
feiyangqingyun37 分钟前
Qt/C++开发监控GB28181系统/取流协议/同时支持udp/tcp被动/tcp主动
c++·qt·udp·gb28181
CV点灯大师39 分钟前
C++算法训练营 Day10 栈与队列(1)
c++·redis·算法
GGBondlctrl1 小时前
【leetcode】递归,回溯思想 + 巧妙解法-解决“N皇后”,以及“解数独”题目
算法·leetcode·n皇后·有效的数独·解数独·映射思想·数学思想
武子康1 小时前
大数据-276 Spark MLib - 基础介绍 机器学习算法 Bagging和Boosting区别 GBDT梯度提升树
大数据·人工智能·算法·机器学习·语言模型·spark-ml·boosting
武子康1 小时前
大数据-277 Spark MLib - 基础介绍 机器学习算法 Gradient Boosting GBDT算法原理 高效实现
大数据·人工智能·算法·机器学习·ai·spark-ml·boosting
成工小白2 小时前
【C++ 】智能指针:内存管理的 “自动导航仪”
开发语言·c++·智能指针
sc写算法2 小时前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json