上一页 1 ··· 4 5 6 7 8 9 10 11 下一页
摘要: ✅做题思路or感想: 用递归后序遍历二叉树,然后从底部开始记录高度为0,然后往上加1。 每一次操作节点的时候比较左右子树的高度,如果有一棵树不符合条件,则直接把-1返回到最顶的根节点 class Solution { public: int getDepth(TreeNode* cur) { if 阅读全文
posted @ 2022-03-28 22:44 北原春希 阅读(35) 评论(0) 推荐(0)
摘要: ✅做题思路or感想: 直接递归到叶子节点,然后更新最小值就好了 这不是和上一题一模一样吗 class Solution { public: int result = 99999; void dfs(TreeNode* root, int sum) { if (root->left == nullpt 阅读全文
posted @ 2022-03-27 10:33 北原春希 阅读(33) 评论(0) 推荐(0)
摘要: ✅做题思路or感想: 直接递归到叶子节点,然后更新最大值就好了 class Solution { public: int result = 1; void dfs(TreeNode* cur, int sum) { if (cur->left == nullptr && cur->right == 阅读全文
posted @ 2022-03-27 10:24 北原春希 阅读(29) 评论(0) 推荐(0)
摘要: ✅做题思路or感想: 这题是要同时遍历左子树和右子树 遍历左子树的外侧和右子树的外侧 遍历左子树的内侧和右子树的内侧 最后判读是否外侧和内侧都是对称的 class Solution { public: bool dfs(TreeNode* left, TreeNode* right) { //处理左 阅读全文
posted @ 2022-03-27 10:15 北原春希 阅读(28) 评论(0) 推荐(0)
摘要: 做题思路or感想: 遍历一遍二叉树,然后将遍历节点的左右节点互换就好了 class Solution { public: void dfs(TreeNode* root) { if (root == nullptr)return; swap(root->left, root->right); //交 阅读全文
posted @ 2022-03-27 09:59 北原春希 阅读(39) 评论(0) 推荐(0)
摘要: 二叉树的层序遍历(bfs) ✅做题思路or感想: 利用队列来实现层序遍历 当明确每一层直接的元素不用区别对待时,其实代码中的size可以不写,但在这里要根据每一层来进行分组,所以要写size 当队列为空时,结束bfs class Solution { public: vector<vector<in 阅读全文
posted @ 2022-03-26 14:03 北原春希 阅读(23) 评论(0) 推荐(0)
摘要: 二叉树的遍历 前序遍历 遍历顺序:中,左,右 代码实现: void dfs(TreeNode* root) { if (root != nullptr)result.push_back(root->val); if (root->left != nullptr)dfs(root->left); if 阅读全文
posted @ 2022-03-26 13:48 北原春希 阅读(40) 评论(0) 推荐(0)
摘要: Virtual Function(虚函数)in c++ 用法: virtual void log() { std::cout << "hello world!" << std::endl; } 当派生类和父类有函数名的冲突时: 可以直接用::来说明所用函数到底是哪一个类的 🍎若父类的重名函数为虚函 阅读全文
posted @ 2022-03-25 22:17 北原春希 阅读(48) 评论(0) 推荐(0)
摘要: enum in c++ enum的实用的定义:给一个值指定一个名称。enums是一种给值命名的方式。 枚举值就是一个整数 用enum的目的:增加程序的可读性 enum的用法:enums [枚举的类名] 枚举中的数是永远递增的,而且变化量为1 enum A { a,b,c //甚至不需要加分号 } / 阅读全文
posted @ 2022-03-25 22:16 北原春希 阅读(39) 评论(0) 推荐(0)
摘要: ✔做题思路or感想: 先说感想:二刷的我又对这道题写了一个小时,可恶 这道题用单调栈比较好理解 用单调栈来储存下标!用下标来计算宽度,用height[i]来算高度 要保持单调栈递减的状态,符合题意 当height[i]=height[st.top()],则要替换掉栈顶的元素,因为这里是需要用最右的柱 阅读全文
posted @ 2022-03-25 21:01 北原春希 阅读(62) 评论(0) 推荐(0)
上一页 1 ··· 4 5 6 7 8 9 10 11 下一页