摘要: 654. 最大二叉树 解题步骤: 1、确定递归函数的参数和返回值 TreeNode* constructMaximumBinaryTree(vector<int>& nums) 2、确定终止条件 1 TreeNode* node = new TreeNode(0); 2 if (nums.size( 阅读全文
posted @ 2022-10-17 20:39 aijiajia0509 阅读(21) 评论(0) 推荐(0)
摘要: 513. 找树左下角的值 1 class Solution { 2 public: 3 int findBottomLeftValue(TreeNode* root) { 4 //创建result用于存储结果 5 int result = 0; 6 queue<TreeNode*> que; 7 i 阅读全文
posted @ 2022-10-12 16:38 aijiajia0509 阅读(25) 评论(0) 推荐(0)
摘要: 110. 平衡二叉树 递归法: 1 class Solution { 2 public: 3 //递归三步走 4 //1、确定返回值和函数参数 5 int getHeight(TreeNode* node){ 6 //2、明确终止条件 7 if (node == nullptr) return 0; 阅读全文
posted @ 2022-10-10 19:54 aijiajia0509 阅读(23) 评论(0) 推荐(0)
摘要: 104. 二叉树的最大深度 迭代法: 1 class Solution { 2 public: 3 int maxDepth(TreeNode* root) { 4 //创建变量用于存储深度 5 int ans = 0; 6 queue<TreeNode*> que; 7 if (root != n 阅读全文
posted @ 2022-10-09 20:22 aijiajia0509 阅读(24) 评论(0) 推荐(0)
摘要: 102. 二叉树的层序遍历 1 class Solution { 2 public: 3 vector<vector<int>> levelOrder(TreeNode* root) { 4 //创建一个队列用于存储每一层的结点 5 queue<TreeNode*> que; 6 if (root 阅读全文
posted @ 2022-10-09 16:36 aijiajia0509 阅读(18) 评论(0) 推荐(0)
摘要: ● 理论基础 1 //二叉树的定义 2 struct TreeNode { 3 int val; 4 TreeNode *left; 5 TreeNode *right; 6 TreeNode(int x) : val(x), left(NULL), right(NULL) {} 7 }; 递归算法 阅读全文
posted @ 2022-10-08 16:27 aijiajia0509 阅读(36) 评论(0) 推荐(0)
摘要: 150. 逆波兰表达式求值 解题步骤: 第一步:创建一个栈; 第二步:遍历整个数组; 第三步:判断遍历到的元素是运算符还是数字; 数字:直接压入栈中 运算符:将栈顶两个元素做相应运算后再压入栈中 第四步:弹出栈顶元素。 1 class Solution { 2 public: 3 int evalR 阅读全文
posted @ 2022-10-06 10:43 aijiajia0509 阅读(21) 评论(0) 推荐(0)
摘要: 232. 用栈实现队列 1 class MyQueue { 2 public: 3 4 //初始化入队栈和出队栈 5 stack<int> stIn; 6 stack<int> stOut; 7 8 MyQueue() { 9 10 } 11 12 //入队 13 void push(int x) 阅读全文
posted @ 2022-10-03 16:05 aijiajia0509 阅读(21) 评论(0) 推荐(0)
摘要: 459. 重复的子字符串 1 class Solution { 2 public: 3 bool repeatedSubstringPattern(string s) { 4 string t = s + s; 5 //掐头去尾 6 t.erase(t.begin()); 7 t.erase(t.e 阅读全文
posted @ 2022-10-03 10:57 aijiajia0509 阅读(21) 评论(0) 推荐(0)
摘要: 344. 反转字符串 1 class Solution { 2 public: 3 void reverseString(vector<char>& s) { 4 for (int i = 0, j = s.size() - 1; i < s.size() / 2; i++, j--){ 5 //交 阅读全文
posted @ 2022-10-02 17:26 aijiajia0509 阅读(24) 评论(0) 推荐(0)