diff --git a/problems/0104.二叉树的最大深度.md b/problems/0104.二叉树的最大深度.md index 46bbbc07..e740900f 100644 --- a/problems/0104.二叉树的最大深度.md +++ b/problems/0104.二叉树的最大深度.md @@ -597,6 +597,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; +} +``` + -----------------------

diff --git a/problems/0222.完全二叉树的节点个数.md b/problems/0222.完全二叉树的节点个数.md index 416b1dba..08187f76 100644 --- a/problems/0222.完全二叉树的节点个数.md +++ b/problems/0222.完全二叉树的节点个数.md @@ -446,7 +446,80 @@ 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; +} +``` + +满二叉树 +```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; +} +``` -----------------------

diff --git a/problems/0226.翻转二叉树.md b/problems/0226.翻转二叉树.md index 6e7c6eae..55f18816 100644 --- a/problems/0226.翻转二叉树.md +++ b/problems/0226.翻转二叉树.md @@ -562,7 +562,7 @@ var invertTree = function(root) { }; ``` -C: +### C: 递归法 ```c struct TreeNode* invertTree(struct TreeNode* root){ @@ -579,6 +579,7 @@ struct TreeNode* invertTree(struct TreeNode* root){ return root; } ``` + 迭代法:深度优先遍历 ```c struct TreeNode* invertTree(struct TreeNode* root){ diff --git a/problems/0654.最大二叉树.md b/problems/0654.最大二叉树.md index 42e6d2c3..6e39c26a 100644 --- a/problems/0654.最大二叉树.md +++ b/problems/0654.最大二叉树.md @@ -353,6 +353,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); +} +``` ----------------------- diff --git a/problems/1047.删除字符串中的所有相邻重复项.md b/problems/1047.删除字符串中的所有相邻重复项.md index 62bf2adb..00d1336f 100644 --- a/problems/1047.删除字符串中的所有相邻重复项.md +++ b/problems/1047.删除字符串中的所有相邻重复项.md @@ -266,6 +266,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; +} +``` -----------------------