/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
#include <cstddef>
class Solution {
public:
TreeNode* ans = nullptr; //最终的链表
TreeNode* pre = nullptr; //指向左子树的最大值
TreeNode* Convert(TreeNode* pRootOfTree) {
//要求:不能创建任何新的节点,只能调整树中节点的指向
//中序遍历是一个有序序列
//中序序列遍历,左根右
InOrder(pRootOfTree);
return ans;
}
void InOrder(TreeNode* pRootOfTree)
{
if(!pRootOfTree) return ;
InOrder(pRootOfTree->left);
if(ans==NULL)
{
ans = pRootOfTree; //递归到最后,ans指向的是最小的那个节点
pre = pRootOfTree;
}
else {
pre->right = pRootOfTree;
pRootOfTree->left = pre;
pre = pRootOfTree;
}
InOrder(pRootOfTree->right);
}
};
标签:pre,pRootOfTree,right,TreeNode,JZ36,链表,二叉树,ans,InOrder
From: https://www.cnblogs.com/H43724334/p/18144613