二叉树 | 顺序链接孩子结点 递归

利用结点的右孩子指针将一个二叉树的叶子节点从左向右链接成一个单链表(head指向第一个,tail指向最后一个)

//自写
typedef struct {
  ElemType data;
  struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;

typedef struct{
  ElemType data;
  struct LNode *next;
}LNode,*LinkList;

L=(LinkList)malloc(sizeod(*LNode));
LNode *head,*tail=NULL,*p;
int step=0;

LinkList Conct_BT(BiTree T){
  if(T->lchild==NULL&&T->rchild==NULL){
    if(step==0){
      head=T; p=head; L->next=head; head->next; step++;
     }else{
      tail=T;  p->next=tail; p=p->next;
     }
}
  Conct_BT(T->lchild);
  Conct_BT(T->rchild);
}

//答案
void link(BiTree *T,LNode *head,LNode *tail){
  if(T!=NULL){
   if(!p->lchild&&p->rchild){
    if(head==NULL){
      head=p; tail=p;
     }else{
      tail->next=p;
      tail=p;
     }
   }
   link(T->lchild,head,tail);
   link(T->rchild,head,tial);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
1、先序遍历二叉树递归算法: ```c void preorderTraversal(struct TreeNode* root) { if (root == NULL) return; printf("%d ", root->val); preorderTraversal(root->left); preorderTraversal(root->right); } ``` 2、中序遍历二叉树递归算法: ```c void inorderTraversal(struct TreeNode* root) { if (root == NULL) return; inorderTraversal(root->left); printf("%d ", root->val); inorderTraversal(root->right); } ``` 3、后序遍历二叉树递归算法: ```c void postorderTraversal(struct TreeNode* root) { if (root == NULL) return; postorderTraversal(root->left); postorderTraversal(root->right); printf("%d ", root->val); } ``` 4、计算二叉树的深度的递归算法: ```c int maxDepth(struct TreeNode* root) { if (root == NULL) return 0; int leftDepth = maxDepth(root->left); int rightDepth = maxDepth(root->right); return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1; } ``` 5、统计二叉树结点个数的递归算法: ```c int countNodes(struct TreeNode* root) { if (root == NULL) return 0; int leftCount = countNodes(root->left); int rightCount = countNodes(root->right); return leftCount + rightCount + 1; } ``` 6、统计二叉树的叶子结点个数的递归算法: ```c int countLeafNodes(struct TreeNode* root) { if (root == NULL) return 0; if (root->left == NULL && root->right == NULL) return 1; int leftCount = countLeafNodes(root->left); int rightCount = countLeafNodes(root->right); return leftCount + rightCount; } ``` 7、设计该二叉树第K层的结点个数: ```c int countNodesAtKLevel(struct TreeNode* root, int k) { if (root == NULL || k < 1) return 0; if (k == 1) return 1; int leftCount = countNodesAtKLevel(root->left, k - 1); int rightCount = countNodesAtKLevel(root->right, k - 1); return leftCount + rightCount; } ``` 8、求该二叉树中所有结点值最大的元素: ```c int findMax(struct TreeNode* root) { if (root == NULL) return INT_MIN; int leftMax = findMax(root->left); int rightMax = findMax(root->right); return max(max(leftMax, rightMax), root->val); } ``` 9、打印二叉树的叶子结点数的递归算法: ```c void printLeafNodeCount(struct TreeNode* root) { printf("Leaf node count: %d\n", countLeafNodes(root)); } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值