https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/
二叉树的处理,将二叉树转换成类似一个单链表的东东,并且原地。
类似于先跟遍历的顺序。
定义函数
TreeNode* subflatten(TreeNode *root)
并将返回值设置为,先转换出来链表的最后一个位置。
struct TreeNode { int val; TreeNode *left;
TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {} }; class Solution { public: void flatten(TreeNode *root) { if(root == NULL) return; subflatten(root); } TreeNode* subflatten(TreeNode *root) { if(root->left == NULL && root->right == NULL) return root; if(root->left == NULL && root->right!= NULL) return subflatten(root->right); //remember root right for temporary TreeNode *tempr = new TreeNode(0); tempr = root->right; root->right = root->left; root->left = NULL; TreeNode *retNode = subflatten(root->right); if(tempr == NULL) return retNode; retNode->right = tempr; TreeNode *retNode2 = subflatten(tempr); return retNode2; } };