标签:leetcode 面试题 algorithm 算法 二叉树
TreeNode *&link(TreeNode *&root) { if(root == NULL || root->left == NULL && root->right == NULL) return root; TreeNode *root_left = NULL; TreeNode *root_right = NULL; root_left = link(root->left); root_right = link(root->right); if(root_left != NULL) { TreeNode *tail = root_left; //find the last one of root->left. while(tail->right) tail = tail->right; tail->right = root_right; } else root_left = root_right; root->right = root_left; root->left = NULL; return root; } void flatten(TreeNode *root) { if(root == NULL) return; root = link(root); }
void preVisit(TreeNode*& pre, TreeNode*root) { if(root == NULL) return; TreeNode *right = root->right; TreeNode *left = root->left; root->left = NULL; if(pre == NULL) pre = root; else { pre->right = root; pre = root; } if(left) preVisit(pre, left); if(right) preVisit(pre, right); }
void flatten(TreeNode* root) { if(root = NULL) return; TreeNode *pre = NULL; preVisit(pre, root); }
【leetcode】Flatten Binary Tree to Linked List,布布扣,bubuko.com
【leetcode】Flatten Binary Tree to Linked List
标签:leetcode 面试题 algorithm 算法 二叉树
原文地址:http://blog.csdn.net/shiquxinkong/article/details/29678217