Flatten Binary Tree to Linked List

https://leetcode.com/problems/flatten-binary-tree-to-linked-list/

二叉树先序遍历,之后做成链表

 1 /**
 2  * Definition for a binary tree node.
 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     void flatten(TreeNode* root) {
13         if(root==NULL)
14             return;
15         stack<TreeNode *> st1;
16         st1.push(root);
17         TreeNode * last=NULL;
18         while(!st1.empty())
19         {
20             TreeNode * temp=st1.top();
21             st1.pop();
22             
23             if(temp->right!=NULL)
24                 st1.push(temp->right);
25             if(temp->left!=NULL)
26                 st1.push(temp->left);
27 
28             temp->left=NULL;
29             temp->right=NULL;
30             if(last!=NULL)
31                 last->right=temp;
32             last=temp;
33         }
34     }
35 };
原文地址:https://www.cnblogs.com/aguai1992/p/4767533.html