递归如何遍历二叉树并计算列表节点的数量?
How does recursion work on traversing through a binary tree, and counting the number of list nodes?
我无法通过以下 URL (https://repl.it/@flowerplowedup/SquigglyVerifiablePaint#main.c) 可视化这些以下打印和大小函数:
假设您输入了输入序列
4 8 2 7 9 1 3 -1
你最终得到一棵看起来像这样的树:
4
/ \
/ \
2 8
/ \ / \
1 3 7 9
print_ascending
的执行看起来像这样:
print_ascending( node(4) )
{
node(4) != NULL
print_ascending( node(4)->left == node(2) )
{
node(2) != NULL
print_ascending( node(2)->left == node(1) )
{
node(1) != NULL
print_ascending( node(1)->left == NULL )
{
NULL == NULL
return
}
print( 1 )
print_ascending( node(1)->right == NULL)
{
NULL == NULL
return
}
return
}
print( 2 )
print_ascending( node(2)->right == node(3))
{
node(3) != NULL
print_ascending( node(3)->left == NULL )
{
NULL = NULL
return
}
print( 3 )
print_ascending( node(3)->right == NULL )
{
NULL = NULL
return
}
}
print( 4 )
print_ascending( node(4)->right == node(8) )
{
node(8) != NULL
print_ascending( node(8)->left == node(7) )
{
node(7) != NULL
print_ascending( node(7)->left == NULL )
{
NULL == NULL
return
}
print( 7 )
print_ascending( node(7)->right == NULL )
{
NULL == NULL
return
}
return
}
print( 8 )
print_ascending( node(8)->right == node(9) )
{
node(9) != NULL
print_ascending( node(9)->left == NULL )
{
NULL == NULL
return
}
print( 9 )
print_ascending( node(9)->right == NULL )
{
NULL == NULL
return
}
return
}
return
}
return
}
return
}
return
希望这有助于可视化正在发生的事情,以及 size
函数中正在发生的事情。递归是您需要一段时间才能理解的概念之一。
我无法通过以下 URL (https://repl.it/@flowerplowedup/SquigglyVerifiablePaint#main.c) 可视化这些以下打印和大小函数:
假设您输入了输入序列
4 8 2 7 9 1 3 -1
你最终得到一棵看起来像这样的树:
4
/ \
/ \
2 8
/ \ / \
1 3 7 9
print_ascending
的执行看起来像这样:
print_ascending( node(4) )
{
node(4) != NULL
print_ascending( node(4)->left == node(2) )
{
node(2) != NULL
print_ascending( node(2)->left == node(1) )
{
node(1) != NULL
print_ascending( node(1)->left == NULL )
{
NULL == NULL
return
}
print( 1 )
print_ascending( node(1)->right == NULL)
{
NULL == NULL
return
}
return
}
print( 2 )
print_ascending( node(2)->right == node(3))
{
node(3) != NULL
print_ascending( node(3)->left == NULL )
{
NULL = NULL
return
}
print( 3 )
print_ascending( node(3)->right == NULL )
{
NULL = NULL
return
}
}
print( 4 )
print_ascending( node(4)->right == node(8) )
{
node(8) != NULL
print_ascending( node(8)->left == node(7) )
{
node(7) != NULL
print_ascending( node(7)->left == NULL )
{
NULL == NULL
return
}
print( 7 )
print_ascending( node(7)->right == NULL )
{
NULL == NULL
return
}
return
}
print( 8 )
print_ascending( node(8)->right == node(9) )
{
node(9) != NULL
print_ascending( node(9)->left == NULL )
{
NULL == NULL
return
}
print( 9 )
print_ascending( node(9)->right == NULL )
{
NULL == NULL
return
}
return
}
return
}
return
}
return
}
return
希望这有助于可视化正在发生的事情,以及 size
函数中正在发生的事情。递归是您需要一段时间才能理解的概念之一。