摘要: 先来一道简单的迷宫模板题 迷宫由 n 行 m 列的单元格组成,每个单元格要么是空地,要么是障碍物。其中1表示空地,可以走通,2表示障碍物。输出从左上角到右下角的最短路径长度。 输入 5 41 1 2 11 1 1 11 1 2 11 2 1 11 1 1 1输出 7 #include <iostre 阅读全文
posted @ 2023-03-18 20:30 芝士可乐 阅读(42) 评论(0) 推荐(0)
摘要: class Solution { public: TreeNode* trimBST(TreeNode* root, int low, int high) { if (root == nullptr ) return nullptr; if (root->val < low) { TreeNode* 阅读全文
posted @ 2023-01-21 14:39 芝士可乐 阅读(24) 评论(0) 推荐(0)
摘要: 235. 二叉搜索树的最近公共祖先 class Solution { public: TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root==NULL)return NULL; if((ro 阅读全文
posted @ 2023-01-19 17:46 芝士可乐 阅读(24) 评论(0) 推荐(0)
摘要: 530. 二叉搜索树的最小绝对差 遇到在二叉搜索树上求什么最值,求差值之类的,都要思考一下二叉搜索树可是有序的,要利用好这一特点。 class Solution { public: vector<int>vec; void getvec(TreeNode* root){ if(root==NULL) 阅读全文
posted @ 2023-01-19 00:19 芝士可乐 阅读(21) 评论(0) 推荐(0)
摘要: 654. 最大二叉树 class Solution { public: TreeNode* constructMaximumBinaryTree(vector<int>& nums) { if(nums.size()==1){ return new TreeNode(nums[0]); } int 阅读全文
posted @ 2023-01-17 14:26 芝士可乐 阅读(20) 评论(0) 推荐(0)
摘要: 513. 找树左下角的值 下面运用层序遍历法比较简单,当遍历到一层时设立一个值去不断覆盖一层的队头,即最左边元素 class Solution { public: int findBottomLeftValue(TreeNode* root) { int leftnum; queue<TreeNod 阅读全文
posted @ 2023-01-15 22:01 芝士可乐 阅读(33) 评论(0) 推荐(0)
摘要: 110. 平衡二叉树 注意概念 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数。 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数。 class Solution { public: int getHeight(TreeNode* root){ if(root==NULL)r 阅读全文
posted @ 2023-01-13 14:41 芝士可乐 阅读(11) 评论(0) 推荐(0)
摘要: 104. 二叉树的最大深度 class Solution { public: int maxDepth(TreeNode* root) { if(root==NULL)return 0; return 1+max(maxDepth(root->left),maxDepth(root->right)) 阅读全文
posted @ 2023-01-13 00:50 芝士可乐 阅读(26) 评论(0) 推荐(0)
摘要: 102. 二叉树的层序遍历 class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vector<int>>result; queue<TreeNode*>qe; if(root!=nullpt 阅读全文
posted @ 2023-01-11 16:32 芝士可乐 阅读(21) 评论(0) 推荐(0)
摘要: 144. 二叉树的前序遍历 class Solution { public: vector<int> v; vector<int> preorderTraversal(TreeNode* root) { if(root==NULL)return v; v.push_back(root->val); 阅读全文
posted @ 2023-01-10 16:56 芝士可乐 阅读(19) 评论(0) 推荐(0)