标题:大神们,请教个比较基础的 问题 关于链表深度的
只看楼主
a515047717
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2018-7-25
 问题点数:0 回复次数:0 
大神们,请教个比较基础的 问题 关于链表深度的
#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,
搜索更多相关主题的帖子: int NULL return gdb if 
2018-07-25 09:39



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-488810-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.361212 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved