https://leetcode.com/problems/flatten-binary-tree-to-linked-list/
!
我個人認為非遞迴比遞迴解法還更好理解,先把左節點移置右節點後把根節點往右節點移動,以此循環值到條件不符合為止。
圖片來源: https://www.cnblogs.com/grandyang/p/4293853.html
void flatten(struct TreeNode* root) {
while(root != NULL) {
if(root->left != NULL) {
struct TreeNode* ptr = root->left;
while(ptr->right != NULL)
ptr = ptr->right;
ptr->right = root->right;
root->right = root->left;
root->left = NULL;
}
root = root->right;
}
}
while(root != null) {
if(root.left != null) {
var ptr = root.left
while(ptr.right != null)
ptr = ptr.right;
ptr.right = root.right;
root.right = root.left;
root.left = null;
}
root = root.right;
}
https://github.com/SIAOYUCHEN/leetcode
https://ithelp.ithome.com.tw/users/20100009/ironman/2500
https://ithelp.ithome.com.tw/users/20113393/ironman/2169
https://ithelp.ithome.com.tw/users/20107480/ironman/2435
https://ithelp.ithome.com.tw/users/20107195/ironman/2382
https://ithelp.ithome.com.tw/users/20119871/ironman/2210
https://ithelp.ithome.com.tw/users/20106426/ironman/2136
Sometimes the right path is not the easiest one.
有時對的那條路,往往是最難走的