「代码随想录算法训练营」第十五天 | 二叉树 part5
654. 最大二叉树
题目链接:https://leetcode.cn/problems/maximum-binary-tree/
题目难度:中等
文章讲解:https://programmercarl.com/0654.最大二叉树.html
视频讲解:https://www.bilibili.com/video/BV1MG411G7ox
题目状态:有思路,但独立完成有点困难,需要复习昨天学的内容
思路:
和昨天的根据中序和前序构造二叉树的思路一样,就是找到数组中的最大值以及其下标,然后分割数组分别放在二叉树的左孩子树和右孩子树上,进行递归。
代码实现:
/** * 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* constructMaximumBinaryTree(vector<int>& nums) { if(nums.size() == 0) return nullptr; int maxValue = *max_element(nums.begin(), nums.end()); TreeNode *root = new TreeNode(maxValue); if(nums.size() == 1) return root; int maxIndex; for(maxIndex = 0; maxIndex < nums.size(); ++maxIndex) { if(nums[maxIndex] == maxValue) break; } vector<int> leftNums(nums.begin(), nums.begin() + maxIndex); vector<int> rightNums(nums.begin() + maxIndex + 1, nums.end()); root->left = constructMaximumBinaryTree(leftNums); root->right = constructMaximumBinaryTree(rightNums); return root; } };
617. 合并二叉树
题目链接:https://leetcode.cn/problems/merge-two-binary-trees/
题目难度:简单
文章讲解:https://programmercarl.com/0617.合并二叉树.html
视频讲解:https://www.bilibili.com/video/BV1m14y1Y7JK
题目状态:哈哈哈哈,学习了递归三件套后,感觉这题真好写,一遍过
思路:
- 递归返回值和参数:返回值就是
TreeNode *
,参数就是两个二叉树的节点。 - 终止条件:
a. 二叉树 1 为nullptr
并且二叉树 2 为nullptr
时,返回nullptr
;
b. 二叉树 1 为nullptr
并且二叉树 2 不为nullptr
时,返回二叉树 2;
c. 二叉树 1 不为nullptr
并且二叉树 2 为nullptr
时,返回二叉树 1。 - 单个循环:创建一个节点,节点的值为二叉树 1 的值+二叉树 2 的值。
代码实现:
/** * 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* mergeTrees(TreeNode* root1, TreeNode* root2) { if(root1 == nullptr && root2 == nullptr) return nullptr; if(root1 == nullptr && root2 != nullptr) return root2; if(root1 != nullptr && root2 == nullptr) return root1; int rootValue = root1->val + root2->val; TreeNode *root = new TreeNode(rootValue); root->left = mergeTrees(root1->left, root2->left); root->right = mergeTrees(root1->right, root2->right); return root; } };
700. 二叉搜索树中的搜索
题目链接:https://leetcode.cn/problems/search-in-a-binary-search-tree/
题目难度:简单
文章讲解:https://programmercarl.com/0700.二叉搜索树中的搜索.html
视频讲解:https://www.bilibili.com/video/BV1wG411g7sF
题目状态:隐藏在脑子里的二叉搜索树的知识又回来了,过
思路:
当当前节点的值大于其要搜索的值,向左遍历;当当前节点的值小于其要搜索的值,向右遍历。
代码实现:
/** * 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* searchBST(TreeNode* root, int val) { if(root == nullptr) return nullptr; if(root->val == val) return root; if(val < root->val) return searchBST(root->left, val); else return searchBST(root->right, val); } };
迭代法代码:
/** * 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* searchBST(TreeNode* root, int val) { while(root != nullptr) { if(root->val > val) root = root->left; else if(root->val < val) root = root->right; else return root; } return nullptr; } };
98. 验证二叉搜索树
题目链接:https://leetcode.cn/problems/validate-binary-search-tree/
题目难度:中等
文章讲解:https://programmercarl.com/0098.验证二叉搜索树.html
视频讲解:https://www.bilibili.com/video/BV18P411n7Q4
题目状态:有错误思路,看题解通过
思路:
使用中序遍历,并利用一个节点来保存每次遍历时的前一个节点,如果前一个节点的值大于当前节点,直接返回false
,直到遍历结束。
代码:
/** * 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 *pre = nullptr; bool isValidBST(TreeNode* root) { if(root == nullptr) return true; bool left = isValidBST(root->left); if(pre != nullptr && pre->val >= root->val) return false; pre = root; bool right = isValidBST(root->right); return left && right; } };
使用数组的方法:将二叉搜索树中的节点按左中右的顺序依次保存到一个数组中,如果是二叉搜索树,这个数组将是有序的,如果不是二叉搜索树,这个数组就是无序的。
/** * 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: vector<int> vec; void traversal(TreeNode *root) { if(root == nullptr) return; traversal(root->left); vec.push_back(root->val); traversal(root->right); } bool isValidBST(TreeNode* root) { traversal(root); for(int i = 1; i < vec.size(); ++i) { if(vec[i] <= vec[i - 1]) return false; } return true; } };
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?