From 808bdbb8604de47f9e75412d0ebb85f92e33fedf Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 8 Nov 2021 11:50:39 +0000 Subject: [PATCH 1/6] =?UTF-8?q?=E6=9B=B4=E6=94=B9=200226.=E7=BF=BB?= =?UTF-8?q?=E8=BD=AC=E4=BA=8C=E5=8F=89=E6=A0=91.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0226.翻转二叉树.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 36083dcd..f9185959 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -565,7 +565,7 @@ var invertTree = function(root) { }; ``` -C: +### C: 递归法 ```c struct TreeNode* invertTree(struct TreeNode* root){ @@ -582,6 +582,7 @@ struct TreeNode* invertTree(struct TreeNode* root){ return root; } ``` + 迭代法:深度优先遍历 ```c struct TreeNode* invertTree(struct TreeNode* root){ From 61044a1f94583e817dbf799e8d5c9b59da800e8b Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 8 Nov 2021 12:20:52 +0000 Subject: [PATCH 2/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0.md=20C=E8=AF=AD=E8=A8=80=E8=BF=AD=E4=BB=A3?= =?UTF-8?q?=E6=B3=95=E3=80=81=E9=80=92=E5=BD=92=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0222.完全二叉树的节点个数.md | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index dc09985d..0826110f 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -449,7 +449,51 @@ var countNodes = function(root) { }; ``` +## C: +递归法 +```c +int countNodes(struct TreeNode* root) { + //若传入结点不存在,返回0 + if(!root) + return 0; + //算出左右子树的结点总数 + int leftCount = countNodes(root->left); + int rightCount = countNodes(root->right); + //返回左右子树结点总数+1 + return leftCount + rightCount + 1; +} +int countNodes(struct TreeNode* root){ + return getNodes(root); +} +``` + +迭代法 +```c +int countNodes(struct TreeNode* root){ + //记录结点总数 + int totalNum = 0; + //开辟栈空间 + struct TreeNode** stack = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 100); + int stackTop = 0; + //如果root结点不为NULL,则将其入栈。若为NULL,则不会进入遍历,返回0 + if(root) + stack[stackTop++] = root; + //若栈中有结点存在,则进行遍历 + while(stackTop) { + //取出栈顶元素 + struct TreeNode* tempNode = stack[--stackTop]; + //结点总数+1 + totalNum++; + //若栈顶结点有左右孩子,将它们入栈 + if(tempNode->left) + stack[stackTop++] = tempNode->left; + if(tempNode->right) + stack[stackTop++] = tempNode->right; + } + return totalNum; +} +``` ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) From f3b140591b67b524e322a0dc31e3043488ea422b Mon Sep 17 00:00:00 2001 From: Arthur Date: Mon, 8 Nov 2021 14:07:24 +0000 Subject: [PATCH 3/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200222.=E5=AE=8C?= =?UTF-8?q?=E5=85=A8=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9A=84=E7=BB=93=E7=82=B9?= =?UTF-8?q?=E4=B8=AA=E6=95=B0.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../0222.完全二叉树的节点个数.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 0826110f..14e1537a 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -495,6 +495,35 @@ int countNodes(struct TreeNode* root){ } ``` +满二叉树 +```c +int countNodes(struct TreeNode* root){ + if(!root) + return 0; + int leftHeight = 0; + int rightHeight = 0; + struct TreeNode* rightNode = root->right; + struct TreeNode* leftNode = root->left; + //求出左子树深度 + while(leftNode) { + leftNode = leftNode->left; + leftHeight++; + } + + //求出右子树深度 + while(rightNode) { + rightNode = rightNode->right; + rightHeight++; + } + //若左右子树深度相同,为满二叉树。结点个数为2^height-1 + if(rightHeight == leftHeight) { + return (2 << leftHeight) - 1; + } + //否则返回左右子树的结点个数+1 + return countNodes(root->right) + countNodes(root->left) + 1; +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From 6d01869bbc4cdbd0034ffb1314556d9147caa132 Mon Sep 17 00:00:00 2001 From: ArthurP Date: Thu, 11 Nov 2021 22:09:25 +0000 Subject: [PATCH 4/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200654.=E6=9C=80?= =?UTF-8?q?=E5=A4=A7=E4=BA=8C=E5=8F=89=E6=A0=91.md=20C=E8=AF=AD=E8=A8=80?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0654.最大二叉树.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index a4ae868a..5f012861 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -356,6 +356,35 @@ var constructMaximumBinaryTree = function (nums) { }; ``` +## C +```c +struct TreeNode* traversal(int* nums, int left, int right) { + //若左边界大于右边界,返回NULL + if(left >= right) + return NULL; + + //找出数组中最大数坐标 + int maxIndex = left; + int i; + for(i = left + 1; i < right; i++) { + if(nums[i] > nums[maxIndex]) + maxIndex = i; + } + + //开辟结点 + struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode)); + //将结点的值设为最大数组数组元素 + node->val = nums[maxIndex]; + //递归定义左孩子结点和右孩子结点 + node->left = traversal(nums, left, maxIndex); + node->right = traversal(nums, maxIndex + 1, right); + return node; +} + +struct TreeNode* constructMaximumBinaryTree(int* nums, int numsSize){ + return traversal(nums, 0, numsSize); +} +``` ----------------------- From c249f3a0f1aff16db83a07322e0c2ef481f466fb Mon Sep 17 00:00:00 2001 From: ArthurP Date: Thu, 11 Nov 2021 22:32:25 +0000 Subject: [PATCH 5/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=200104.=E4=BA=8C?= =?UTF-8?q?=E5=8F=89=E6=A0=91=E7=9A=84=E6=9C=80=E5=A4=A7=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?.md=20C=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0104.二叉树的最大深度.md | 55 +++++++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index e20f147f..1c94994f 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -582,6 +582,61 @@ var maxDepth = function(root) { }; ``` +## C +二叉树最大深度递归 +```c +int maxDepth(struct TreeNode* root){ + //若传入结点为NULL,返回0 + if(!root) + return 0; + + //求出左子树深度 + int left = maxDepth(root->left); + //求出右子树深度 + int right = maxDepth(root->right); + //求出左子树深度和右子树深度的较大值 + int max = left > right ? left : right; + //返回较大值+1(1为当前层数) + return max + 1; +} +``` +二叉树最大深度迭代 +```c +int maxDepth(struct TreeNode* root){ + //若传入根节点为NULL,返回0 + if(!root) + return 0; + + int depth = 0; + //开辟队列空间 + struct TreeNode** queue = (struct TreeNode**)malloc(sizeof(struct TreeNode*) * 6000); + int queueFront = 0; + int queueEnd = 0; + + //将根结点入队 + queue[queueEnd++] = root; + + int queueSize; + //求出当前队列中元素个数 + while(queueSize = queueEnd - queueFront) { + int i; + //若当前队列中结点有左右子树,则将它们的左右子树入队 + for(i = 0; i < queueSize; i++) { + struct TreeNode* tempNode = queue[queueFront + i]; + if(tempNode->left) + queue[queueEnd++] = tempNode->left; + if(tempNode->right) + queue[queueEnd++] = tempNode->right; + } + //更新队头下标 + queueFront += queueSize; + //深度+1 + depth++; + } + return depth; +} +``` + ----------------------- * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * B站视频:[代码随想录](https://space.bilibili.com/525438321) From c46acca1e4ceaddbda6b4975afe0c4d061ab11fc Mon Sep 17 00:00:00 2001 From: ArthurP Date: Sun, 14 Nov 2021 10:05:29 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=201047.=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=AD=97=E7=AC=A6=E4=B8=B2=E4=B8=AD=E7=9A=84=E6=89=80?= =?UTF-8?q?=E6=9C=89=E7=9B=B8=E9=82=BB=E9=87=8D=E5=A4=8D=E9=A1=B9.md=20C?= =?UTF-8?q?=E8=AF=AD=E8=A8=80=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...除字符串中的所有相邻重复项.md | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index f70f39f3..ef93ced8 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -269,6 +269,57 @@ var removeDuplicates = function(s) { }; ``` +C: +方法一:使用栈 +```c +char * removeDuplicates(char * s){ + //求出字符串长度 + int strLength = strlen(s); + //开辟栈空间。栈空间长度应为字符串长度+1(为了存放字符串结束标志'\0') + char* stack = (char*)malloc(sizeof(char) * strLength + 1); + int stackTop = 0; + + int index = 0; + //遍历整个字符串 + while(index < strLength) { + //取出当前index对应字母,之后index+1 + char letter = s[index++]; + //若栈中有元素,且栈顶字母等于当前字母(两字母相邻)。将栈顶元素弹出 + if(stackTop > 0 && letter == stack[stackTop - 1]) + stackTop--; + //否则将字母入栈 + else + stack[stackTop++] = letter; + } + //存放字符串结束标志'\0' + stack[stackTop] = '\0'; + //返回栈本身作为字符串 + return stack; +} +``` +方法二:双指针法 +```c +char * removeDuplicates(char * s){ + //创建快慢指针 + int fast = 0; + int slow = 0; + //求出字符串长度 + int strLength = strlen(s); + //遍历字符串 + while(fast < strLength) { + //将当前slow指向字符改为fast指向字符。fast指针+1 + char letter = s[slow] = s[fast++]; + //若慢指针大于0,且慢指针指向元素等于字符串中前一位元素,删除慢指针指向当前元素 + if(slow > 0 && letter == s[slow - 1]) + slow--; + else + slow++; + } + //在字符串结束加入字符串结束标志'\0' + s[slow] = 0; + return s; +} +``` -----------------------