// 56ms 如果不加 root->left=NULL; 则出现runtime error 应该是结束之后 这样的树有左节点 所以 程序比较两个树是否相同时用了 NULL->left
1 /**
2 * Definition for binary tree
3 * struct TreeNode {
4 * int val;
5 * TreeNode *left;
6 * TreeNode *right;
7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8 * };
9 */
10 class Solution {
11 public:
12
13 void ReFlat(TreeNode *root,TreeNode **head,TreeNode **end)
14 {
15 TreeNode *leftHead,*leftEnd,*rightHead,*rightEnd;
16 *head=root;
17 *end=root;
18 leftHead=leftEnd=rightHead=rightEnd=NULL;
19 if(root->left)
20 {
21 ReFlat(root->left,&leftHead,&leftEnd);
22 }
23 if(root->right)
24 {
25 ReFlat(root->right,&rightHead,&rightEnd);
26 }
27 if(leftHead)
28 {
29 (*end)->right=leftHead;
30 *end=leftEnd;
31 }
32 if(rightHead)
33 {
34 (*end)->right=rightHead;
35 (*end)=rightEnd;
36 }
37 root->left=NULL;
38 }
39 void flatten(TreeNode *root) {
40 // Start typing your C/C++ solution below
41 // DO NOT write int main() function
42
43 TreeNode *head,*end;
44 head=end=NULL;
45 if(root==NULL)
46 return;
47 ReFlat(root,&head,&end);
48
49
50
51 }
52 };