File tree Expand file tree Collapse file tree 2 files changed +77
-1
lines changed Expand file tree Collapse file tree 2 files changed +77
-1
lines changed Original file line number Diff line number Diff line change @@ -829,7 +829,42 @@ int maxDepth(struct TreeNode* root){
829829 return depth;
830830}
831831```
832-
832+ 二叉树最大深度迭代——后序遍历实现
833+ ``` c
834+ int maxDepth (struct TreeNode * root)
835+ {
836+ if(root == NULL)
837+ return 0;
838+ struct TreeNode * stack[ 10000] = {};
839+ int top = -1;
840+ struct TreeNode * p = root, * r = NULL; // r指向上一个被访问的结点
841+ int depth = 0, maxDepth = -1;
842+ while(p != NULL || top >= 0)
843+ {
844+ if(p != NULL)
845+ {
846+ stack[ ++top] = p;
847+ depth++;
848+ p = p->left;
849+ }
850+ else
851+ {
852+ p = stack[ top] ;
853+ if(p->right != NULL && p->right != r) // 右子树未被访问
854+ p = p->right;
855+ else
856+ {
857+ if(depth >= maxDepth) maxDepth = depth;
858+ p = stack[ top--] ;
859+ depth--;
860+ r = p;
861+ p = NULL;
862+ }
863+ }
864+ }
865+ return maxDepth;
866+ }
867+ ```
833868### Swift:
834869
835870104.二叉树的最大深度
Original file line number Diff line number Diff line change @@ -253,6 +253,47 @@ func main(){
253253```
254254
255255
256+ ### C:
257+ ``` C
258+ #include < stdio.h>
259+ #include < string.h>
260+
261+ void reverse (char * s, int left, int right)
262+ {
263+ while(left <= right)
264+ {
265+ char c = s[ left] ;
266+ s[ left] = s[ right] ;
267+ s[ right] = c;
268+ left++;
269+ right--;
270+ }
271+ }
272+
273+ void rightRotate(char * s, int k)
274+ {
275+ int len = strlen(s);
276+ // 先局部反转再整体反转
277+ reverse(s, 0, len - k - 1); // 反转前部分
278+ reverse(s, len - k, len - 1); // 反转后部分:后k位
279+ reverse(s, 0, len - 1); // 整体反转
280+ }
281+
282+ int main()
283+ {
284+
285+ int k;
286+ scanf("%d", &k);
287+ char s[10000];
288+ scanf("%s", s);
289+
290+ rightRotate(s, k);
291+ printf("%s\n", s);
292+
293+ return 0;
294+ }
295+ ```
296+
256297### JavaScript:
257298
258299
You can’t perform that action at this time.
0 commit comments