代码实现:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ void convertBiNode_pro(struct TreeNode *root, struct TreeNode **p) { if (root) { convertBiNode_pro(root->left, p); (*p)->right = root; (*p) = (*p)->right; (*p)->left = NULL; convertBiNode_pro(root->right, p); } } struct TreeNode* convertBiNode(struct TreeNode *root) { if (root == NULL) { return root; } struct TreeNode *h = root; while (h->left) { // 找到链表的头 h = h->left; } struct TreeNode **p = malloc(sizeof(struct TreeNode*)); *p = malloc(sizeof(struct TreeNode)); convertBiNode_pro(root, p); // 递归,中序遍历 return h; }