您的位置:首页 > 其它

【LeetCode】Flatten Binary Tree to Linked List

2015-03-05 11:49 274 查看
/**
* Definition for binary tree
* struct TreeNode {
*     int val;
*     TreeNode *left;
*     TreeNode *right;
*     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void flatten(TreeNode *root) {
stack<TreeNode*>tree;
if (root == NULL)
return;
tree.push(root);
while (tree.size())
{
auto p = tree.top();
tree.pop();
if (p->right)
tree.push(p->right);
if (p->left)
tree.push(p->left);

p->left = NULL;
if (tree.size())
{
p->right = tree.top();
}
}
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: