/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
//理解递归的好例子
class Solution {
public:
void hConvert(TreeNode* pRoot)
{
if(pRoot!=NULL){
if(pRoot->left!=NULL&&pRoot->left->right!=pRoot)
hConvert(pRoot->left);
TreeNode *temp=pRoot->right ;
TreeNode* post=pRoot->right;
if(post!=NULL){
while(post->left)
post=post->left;
pRoot->right=post;
post->left=pRoot;
}
TreeNode* pre=pRoot->left;
if(pre!=NULL){
while(pre->right&&pre->right!=pRoot)
pre=pre->right;
pRoot->left=pre;
pre->right=pRoot;
}
hConvert(temp);
}
}
TreeNode* Convert(TreeNode* pRootOfTree){
if(pRootOfTree==NULL) return NULL;
TreeNode* pRoot=pRootOfTree;
while(pRootOfTree->left)
pRootOfTree=pRootOfTree->left;
hConvert(pRoot);
return pRootOfTree;
}
};
剑指Offer:二叉搜索树与双向链表
最新推荐文章于 2022-05-13 21:54:01 发布