我的主页:Lei宝啊
愿所有美好如期而遇
二叉树的递归是二叉树很重要的问题,几乎解决二叉树的问题都要使用递归,接下来我们将解决二叉树几个最基础的递归问题。
目录
- typedef struct BT_Tree
- {
- int data;
- struct BT_Tree* left;
- struct BT_Tree* right;
- }BT_Tree;
- void PrevOrder(BT_Tree* node)
- {
- if (node == NULL)
- return;
-
- printf("%d ", node->data);
- PrevOrder(node->left);
- PrevOrder(node->right);
- }
黄瓜个图看看:
- void InOrder(BT_Tree* node)
- {
- if (node == NULL)
- return;
-
- PrevOrder(node->left);
- printf("%d ", node->data);
- PrevOrder(node->right);
- }
花瓜个图看看:
- void LastOrder(BT_Tree* node)
- {
- if (node == NULL)
- return;
-
- PrevOrder(node->left);
- PrevOrder(node->right);
- printf("%d ", node->data);
- }
画个图看看:
- int SizeofNode(BT_Tree* node)
- {
- if (node == NULL)
- return 0;
-
- return SizeofNode(node->left) + SizeofNode(node->right) + 1;
- }
画个图:
- int SizeofLeaf(BT_Tree* node)
- {
- if (node == NULL)
- return 0;
- if (node->left == NULL && node->right == NULL)
- return 1;
-
- return SizeofLeaf(node->left) + SizeofLeaf(node->right);
- }
画图:
- int SizeInLineKNode(BT_Tree* node, int k)
- {
- if (node == NULL)
- return 0;
-
- if (k == 1)
- return 1;
-
- return SizeInLineKNode(node->left, k - 1) + SizeInLineKNode(node->right, k - 1);
- }
图: