我越来越菜了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
class Solution { public: void flatten(TreeNode* root) { TreeNode* now = root; while(now) { if(now->left) { TreeNode* pre = now->left; while(pre->right) { pre = pre->right; } pre->right = now->right; now->right = now->left; now->left = nullptr; } now = now->right; } } };
|
太强了
review
和上面区别就是他留了一部分左子树给后面处理,而我就直接处理完了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
class Solution { public: void flatten(TreeNode* root) { TreeNode* pt = root; while(pt) { if(pt->left) { auto cur = pt->left; TreeNode* pre; while(cur) { pre = cur; if(cur->right) cur = cur->right; else cur = cur->left; } pre->right = pt->right; pt->right = pt->left; pt->left = nullptr; } pt = pt->right; } } };
|