From e3a944cacc087ed3e5a418107098891d5abe2dcf Mon Sep 17 00:00:00 2001 From: youngyangyang04 <826123027@qq.com> Date: Fri, 7 Aug 2020 09:21:17 +0800 Subject: [PATCH] Update --- README.md | 3 ++ problems/0094.二叉树的中序遍历.md | 2 + problems/0102.二叉树的层序遍历.md | 34 ++++++++++++++ problems/0145.二叉树的后序遍历.md | 12 ++--- problems/0199.二叉树的右视图.md | 32 +++++++++++++ problems/0202.快乐数.md | 2 +- .../0222.完全二叉树的节点个数.md | 46 +++++++++++++++++++ problems/0226.翻转二叉树.md | 22 ++++++++- 8 files changed, 145 insertions(+), 8 deletions(-) create mode 100644 problems/0102.二叉树的层序遍历.md create mode 100644 problems/0199.二叉树的右视图.md create mode 100644 problems/0222.完全二叉树的节点个数.md diff --git a/README.md b/README.md index b0c9c31a..1290b29f 100644 --- a/README.md +++ b/README.md @@ -97,6 +97,7 @@ |[0098.验证二叉搜索树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0098.验证二叉搜索树.md) |树 |中等|**递归**| |[0100.相同的树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0100.相同的树.md) |树 |简单|**递归** | |[0101.对称二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0101.对称二叉树.md) |树 |简单|**递归** **迭代/队列/栈**| +|[0102.二叉树的层序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0102.二叉树的层序遍历.md) |树 |中等|**广度优先搜索/队列**| |[0104.二叉树的最大深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0104.二叉树的最大深度.md) |树 |简单|**递归** **迭代/队列/BFS**| |[0110.平衡二叉树](https://github.com/youngyangyang04/leetcode/blob/master/problems/0110.平衡二叉树.md) |树 |简单|**递归**| |[0111.二叉树的最小深度](https://github.com/youngyangyang04/leetcode/blob/master/problems/0111.二叉树的最小深度.md) |树 |简单|**递归** **队列/BFS**| @@ -105,12 +106,14 @@ |[0145.二叉树的后序遍历](https://github.com/youngyangyang04/leetcode/blob/master/problems/0145.二叉树的后序遍历.md) |树 |困难|**递归** **迭代/栈**| |[0151.翻转字符串里的单词](https://github.com/youngyangyang04/leetcode/blob/master/problems/0151.翻转字符串里的单词.md) |字符串 |中等|**模拟/双指针**| |[0155.最小栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0155.最小栈.md) |栈 |简单|**栈**| +|[0199.二叉树的右视图](https://github.com/youngyangyang04/leetcode/blob/master/problems/0199.二叉树的右视图.md) |二叉树 |中等|**广度优先遍历/队列**| |[0202.快乐数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0202.快乐数.md) |哈希表 |简单|**哈希**| |[0203.移除链表元素](https://github.com/youngyangyang04/leetcode/blob/master/problems/0203.移除链表元素.md) |链表 |简单|**模拟** **虚拟头结点**| |[0205.同构字符串](https://github.com/youngyangyang04/leetcode/blob/master/problems/0205.同构字符串.md) |哈希表 |简单| **哈希**| |[0206.翻转链表](https://github.com/youngyangyang04/leetcode/blob/master/problems/0206.翻转链表.md) |链表 |简单| **模拟** **递归**| |[0209.长度最小的子数组](https://github.com/youngyangyang04/leetcode/blob/master/problems/0209.长度最小的子数组.md) |数组 |中等| **暴力** **滑动窗口**| |[0219.存在重复元素II](https://github.com/youngyangyang04/leetcode/blob/master/problems/0219.存在重复元素II.md) | 哈希表 |简单| **哈希** | +|[0222.完全二叉树的节点个数](https://github.com/youngyangyang04/leetcode/blob/master/problems/0222.完全二叉树的节点个数.md) | 树 |简单| **递归** | |[0225.用队列实现栈](https://github.com/youngyangyang04/leetcode/blob/master/problems/0225.用队列实现栈.md) | 队列 |简单| **队列** | |[0232.用栈实现队列](https://github.com/youngyangyang04/leetcode/blob/master/problems/0232.用栈实现队列.md) | 栈 |简单| **栈** | |[0237.删除链表中的节点](https://github.com/youngyangyang04/leetcode/blob/master/problems/0237.删除链表中的节点.md) |链表 |简单| **原链表移除** **添加虚拟节点** 递归| diff --git a/problems/0094.二叉树的中序遍历.md b/problems/0094.二叉树的中序遍历.md index 9017952c..75b3716e 100644 --- a/problems/0094.二叉树的中序遍历.md +++ b/problems/0094.二叉树的中序遍历.md @@ -66,8 +66,10 @@ public: if (node != NULL) { st.pop(); // 将该节点弹出,避免重复操作,下面再将右中左节点添加到栈中 if (node->right) st.push(node->right); // 添加右节点 + st.push(node); // 添加中节点 st.push(NULL); // 中节点访问过,但是还没有处理,需要做一下标记。 + if (node->left) st.push(node->left); // 添加左节点 } else { st.pop(); // 将空节点弹出 diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md new file mode 100644 index 00000000..daa19ac1 --- /dev/null +++ b/problems/0102.二叉树的层序遍历.md @@ -0,0 +1,34 @@ +## 题目地址 +https://leetcode-cn.com/problems/binary-tree-level-order-traversal/ + +## 思路 + +使用队列实现广度优先遍历 + +## C++代码 + +``` +class Solution { +public: + vector> levelOrder(TreeNode* root) { + queue que; + if (root != NULL) que.push(root); + vector> result; + while (!que.empty()) { + int size = que.size(); + vector vec; + for (int i = 0; i < size; i++) {// 这里一定要使用固定大小size,不要使用que.size() + TreeNode* node = que.front(); + que.pop(); + vec.push_back(node->val); + if (node->left) que.push(node->left); + if (node->right) que.push(node->right); + } + result.push_back(vec); + } + return result; + } +}; +``` + +> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。 diff --git a/problems/0145.二叉树的后序遍历.md b/problems/0145.二叉树的后序遍历.md index 9d89419e..87b52dc3 100644 --- a/problems/0145.二叉树的后序遍历.md +++ b/problems/0145.二叉树的后序遍历.md @@ -48,10 +48,10 @@ public: st.pop(); if (node != NULL) result.push_back(node->val); else continue; - st.push(node->left); + st.push(node->left); // 相对于前序遍历,这更改一下入栈顺序 st.push(node->right); } - reverse(result.begin(), result.end()); + reverse(result.begin(), result.end()); // 将结果反转之后就是左右中的顺序了 return result; } }; @@ -63,7 +63,6 @@ public: ``` class Solution { public: - vector postorderTraversal(TreeNode* root) { vector result; stack st; @@ -72,10 +71,11 @@ public: TreeNode* node = st.top(); if (node != NULL) { st.pop(); - st.push(node); + st.push(node); // 中 st.push(NULL); - if (node->right) st.push(node->right); - if (node->left) st.push(node->left); + + if (node->right) st.push(node->right); // 右 + if (node->left) st.push(node->left); // 左 } else { st.pop(); diff --git a/problems/0199.二叉树的右视图.md b/problems/0199.二叉树的右视图.md new file mode 100644 index 00000000..a99d428b --- /dev/null +++ b/problems/0199.二叉树的右视图.md @@ -0,0 +1,32 @@ +## 题目地址 +https://leetcode-cn.com/problems/binary-tree-right-side-view/ + +## 思路 + +广度优先搜索模板题目,层序遍历的时候,将每一层的最后元素放入result数组中 + +## C++代码 + +``` +class Solution { +public: + vector rightSideView(TreeNode* root) { + queue que; + if (root != NULL) que.push(root); + vector result; + while (!que.empty()) { + int size = que.size(); + for (int i = 0; i < size; i++) { + TreeNode* node = que.front(); + que.pop(); + if (i == (size - 1)) result.push_back(node->val);//将每一层的最后元素放入result数组中 + if (node->left) que.push(node->left); + if (node->right) que.push(node->right); + } + } + return result; + } +}; +``` + +> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。 diff --git a/problems/0202.快乐数.md b/problems/0202.快乐数.md index 38299b96..0a5b6108 100644 --- a/problems/0202.快乐数.md +++ b/problems/0202.快乐数.md @@ -39,5 +39,5 @@ public: } }; ``` -> 更过算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。 +> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。 diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md new file mode 100644 index 00000000..1f1b922d --- /dev/null +++ b/problems/0222.完全二叉树的节点个数.md @@ -0,0 +1,46 @@ +## 题目地址 +https://leetcode-cn.com/problems/count-complete-tree-nodes/ + +## 思路 + +递归题目 + +## C++代码 + +### 递归 +``` +class Solution { +public: + int countNodes(TreeNode* root) { + if (root == NULL) return 0; + return 1 + countNodes(root->left) + countNodes(root->right); + } +}; +``` + +### 迭代-广度优先 + +``` +class Solution { +public: + int countNodes(TreeNode* root) { + queue que; + if (root != NULL) que.push(root); + int count = 0; + int result = 0; + while (!que.empty()) { + int size = que.size(); + for (int i = 0; i < size; i++) { + TreeNode* node = que.front(); + que.pop(); + result++; + if (node->left) que.push(node->left); + if (node->right) que.push(node->right); + } + } + return result; + } +}; +``` + +> 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。 diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index f5fbc836..fd411101 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -3,10 +3,11 @@ https://leetcode-cn.com/problems/invert-binary-tree/ ## 思路 -递归的过程,交换左右节点。 +写递归算法的时候,要想一想是采用前中后序那种遍历方式 ## C++代码 +### 递归(前序遍历) ``` class Solution { public: @@ -19,5 +20,24 @@ public: } }; ``` +### 迭代法(前序遍历) +``` +class Solution { +public: + TreeNode* invertTree(TreeNode* root) { + if (root == NULL) return root; + stack st; + st.push(root); + while(!st.empty()) { + TreeNode* node = st.top(); + st.pop(); + swap(node->left, node->right); + if(node->left) st.push(node->left); + if(node->right) st.push(node->right); + } + return root; + } +}; +``` > 更多算法干货文章持续更新,可以微信搜索「代码随想录」第一时间围观,关注后,回复「Java」「C++」 「python」「简历模板」「数据结构与算法」等等,就可以获得我多年整理的学习资料。