BM30. 二叉搜索树与双向链表
描述
输入描述
二叉树的根节点输出描述
双向链表的其中一个头节点。示例1
输入:
{10,6,14,4,8,12,16}
输出:
From left to right are:4,6,8,10,12,14,16;From right to left are:16,14,12,10,8,6,4;
说明:
输入题面图中二叉树,输出的时候将双向链表的头节点返回即可。示例2
输入:
{5,4,#,3,#,2,#,1}
输出:
From left to right are:1,2,3,4,5;From right to left are:5,4,3,2,1;
说明:
5 / 4 / 3 / 2 / 1 树的形状如上图C 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2020-10-28
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * * @param pRootOfTree TreeNode类 * @return TreeNode类 */ struct TreeNode* Convert(struct TreeNode* pRootOfTree ) { if(!pRootOfTree) return pRootOfTree; struct TreeNode* p=pRootOfTree; if(pRootOfTree->left) { Convert(pRootOfTree->left); p=pRootOfTree->left; while(p->right) p=p->right; pRootOfTree->left=p; p->right=pRootOfTree; } if(pRootOfTree->right) { Convert(pRootOfTree->right); p=pRootOfTree->right; while(p->left) p=p->left; pRootOfTree->right=p; p->left=pRootOfTree; } while(p->left) p=p->left; return p; }
C 解法, 执行用时: 2ms, 内存消耗: 328KB, 提交时间: 2020-08-23
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * * @param pRootOfTree TreeNode类 * @return TreeNode类 */ void ConvertList(struct TreeNode *t, struct TreeNode **pre) { if(t == NULL) return; ConvertList(t->left, pre); t->left = *pre; if(*pre != NULL) (*pre)->right = t; *pre = t; ConvertList(t->right, pre); } struct TreeNode* Convert(struct TreeNode* pRootOfTree ) { // write code here if(pRootOfTree == NULL) return NULL; struct TreeNode *pre=NULL; ConvertList(pRootOfTree,&pre); struct TreeNode* head=pre; while(head->left!=NULL){ head=head->left; } return head; }
C 解法, 执行用时: 2ms, 内存消耗: 332KB, 提交时间: 2021-05-04
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ static struct TreeNode *pre; void my_DG(struct TreeNode *pRoot) { if( !pRoot ) return ; my_DG( pRoot->left ); pRoot->left = pre; if(pre) pre->right = pRoot; pre = pRoot; my_DG( pRoot->right ); } /** * * @param pRootOfTree TreeNode类 * @return TreeNode类 */ struct TreeNode* Convert(struct TreeNode* pRootOfTree ) { // write code here if( !pRootOfTree ) return NULL; pre = NULL; my_DG(pRootOfTree); while(pRootOfTree->left) pRootOfTree = pRootOfTree->left; return pRootOfTree; }
C 解法, 执行用时: 2ms, 内存消耗: 336KB, 提交时间: 2020-09-05
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * * @param pRootOfTree TreeNode类 * @return TreeNode类 */ void in_order(struct TreeNode*root,struct TreeNode**new_list){ if(!root) return; else{ in_order(root->left,new_list); root->left=*new_list; if(*new_list){ (*new_list)->right=root; } (*new_list)=root; in_order(root->right,new_list); } } struct TreeNode* Convert(struct TreeNode* pRootOfTree ) { // write code here struct TreeNode*new_list=NULL; in_order(pRootOfTree,&new_list); while(new_list&&new_list->left) new_list=new_list->left; return new_list; }
C 解法, 执行用时: 2ms, 内存消耗: 340KB, 提交时间: 2020-11-29
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * * @param pRootOfTree TreeNode类 * @return TreeNode类 */ void ConvertList(struct TreeNode *t,struct TreeNode **pre) { if(t==NULL) return ; ConvertList(t->left,pre); t->left=*pre; if(*pre!=NULL) (*pre)->right=t; *pre=t; ConvertList(t->right,pre); } struct TreeNode* Convert(struct TreeNode* pRootOfTree ) { // write code here if(pRootOfTree==NULL) return NULL; struct TreeNode *pre=NULL; ConvertList(pRootOfTree,&pre); struct TreeNode *pHead=pre; while(pHead->left!=NULL) pHead=pHead->left; return pHead; }