/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode* root) {
if (!root) return;
f(root);
while (root)
{
//cout << root->val << "\t";
root->right = root->left, root->left = nullptr;
root = root->right;
}
}
void f(TreeNode* root) {
if (!root) return;
if (_pre == nullptr)
{
_pre = root;
}
else
{
_pre->left = root;
_pre = root;
}
f(root->left);
f(root->right);
}
TreeNode* _pre = nullptr;
};
leetcode114 二叉树展开为链表
最新推荐文章于 2024-08-21 14:09:23 发布
本文介绍了一种将二叉树结构转换为链表的算法,通过修改树节点的左右指针,使得所有节点按中序遍历顺序连接成单链表,此过程不使用额外的数据结构。
499

被折叠的 条评论
为什么被折叠?



