linked list為單調(diào)遞增亭引,說(shuō)明是前序遍歷左右子樹后的合并
1
/
2 5
/ \
3 4 6
case1:
2->3->4
5->6
root->left = "2"
root->rihjt = "5"
找到root->left的結(jié)尾嚼松,并指向root->right
root->right 指向root->left的頭,同時(shí)root->left設(shè)為NULL
case2:
當(dāng)root->left為空
root->right指向的鏈表 為5->6翠霍,不需要任何操作。
void flatten(struct TreeNode* root){
if(root){
flatten(root->left);
flatten(root->right);
if(root->left){
struct TreeNode * p = root->left;
while(p->right){
p = p->right;
}
p->right = root->right;
root->right = root->left;
root->left = NULL;
}
}
}