大神们,请教个比较基础的 问题 关于链表深度的
#include <stdio.h>#include <stdlib.h>
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
int maxDepth(struct TreeNode* root) {
if (root == NULL) return 0;
int leftDepth = maxDepth(root->left);
int rightDepth = maxDepth(root->right);
if (leftDepth > rightDepth) return leftDepth + 1;
else return rightDepth + 1;s
}
int main(void)
{
struct TreeNode root;
root.val = 3;
struct TreeNode node1[2];
node1[0].val = 9;
node1[1].val = 20;
root.left = &node1[0];
root.right = &node1[1];
node1[0].left = NULL;
node1[0].right = NULL;
node1[1].left = NULL;
node1[1].right = NULL;
int depth = maxDepth(&root);
printf("the number is %d \n",depth);
return 0;
}
Breakpoint 1, main () at 133.c:36
36 int depth = maxDepth(&root);
(gdb) display leftDepth
No symbol "leftDepth" in current context.
(gdb) display rightDepth
No symbol "rightDepth" in current context.
(gdb) s
maxDepth (root=0x7fffffffe4c0) at 133.c:11
11 if (root == NULL) return 0;
(gdb) s
14 int leftDepth = maxDepth(root->left);
(gdb) display leftDepth
1: leftDepth = 32767
(gdb) display rightDepth
2: rightDepth = -139736899
(gdb) s
maxDepth (root=0x7fffffffe490) at 133.c:11
11 if (root == NULL) return 0;
2: rightDepth = -134244776
1: leftDepth = 32767
(gdb) s
14 int leftDepth = maxDepth(root->left);
2: rightDepth = -134244776
1: leftDepth = 32767
(gdb) s
maxDepth (root=0x0) at 133.c:11
11 if (root == NULL) return 0;
2: rightDepth = -7120
1: leftDepth = 32767
(gdb) s
19 }
2: rightDepth = -7120
1: leftDepth = 32767
(gdb) s
15 int rightDepth = maxDepth(root->right);
2: rightDepth = -134244776
1: leftDepth = 0
(gdb) s
maxDepth (root=0x0) at 133.c:11
11 if (root == NULL) return 0;
2: rightDepth = -7120
1: leftDepth = 32767
(gdb) s
19 }
2: rightDepth = -7120
1: leftDepth = 32767
(gdb) s
17 if (leftDepth > rightDepth) return leftDepth + 1;
2: rightDepth = 0
1: leftDepth = 0
(gdb) s
18 else return rightDepth + 1;
2: rightDepth = 0
1: leftDepth = 0
(gdb) s
19 }
2: rightDepth = 0
1: leftDepth = 0
(gdb) s
15 int rightDepth = maxDepth(root->right);
2: rightDepth = -139736899
1: leftDepth = 1
(gdb) s
maxDepth (root=0x0) at 133.c:11
11 if (root == NULL) return 0;
2: rightDepth = 0
1: leftDepth = 0
(gdb) s
19 }
2: rightDepth = 0 ***********************
1: leftDepth = 0 ***********************
(gdb) s
17 if (leftDepth > rightDepth) return leftDepth + 1;
2: rightDepth = 0 ************************
1: leftDepth = 1 ************************ 这里为什么上面俩个变量还都0,到了判断之后,确是leftDepth加1 呢
(gdb) s
19 }
2: rightDepth = 0
1: leftDepth = 1
(gdb) s
main () at 133.c:38
38 printf("the number is %d \n",depth);
(gdb) s
the number is 2
40 return 0;
(gdb) s
41 }
(gdb) s
0x00007ffff7a3db35 in __libc_start_main () from /lib64/libc.so.6
(gdb) s
Single stepping until exit from function __libc_start_main,