题目:https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/submissions/
/**
* 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) {
/*
*从根节点开始遍历,如果当前点有左节点,
*则将其对应的左子树这一块并入到其有右
*子树上。
*/
while(root != nullptr) {
if(root->left != nullptr) {
TreeNode *most_right = root->left;
while(most_right->right != nullptr) most_right = most_right->right;
most_right->right = root->right;
root->right = root->left;
root->left = nullptr;
}
root = root->right;
}
}
};