欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

114. 二叉树展开为链表

程序员文章站 2022-03-03 11:24:48
...
class Solution {
public:
void flatten(TreeNode* root)
{
	if (root == NULL)
		return;
	TreeNode* now = root;
	TreeNode* pre;
	while (now)
	{
		if (now->left)
		{
			pre = now->left;
			while (pre->right)
			{
				pre = pre->right;
			}
			//当年节点的右节点挂在当前节点的左子树最右边的孩子上
			pre->right = now->right;
			//当前节点的左子树变为右子树,左子树置为NULL
			now->right = now->left;
			now->left = NULL;
		}
		now = now->right;
	}
}
};