Flatten Binary Tree to Linked List Solutions in C++
Number 114
Difficulty Medium
Acceptance 49.4%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/// Author : Hao Chen// Date : 2014-07-03/*** 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) {vector<TreeNode*> v, stack;stack.push_back(root);while(stack.size()>0){TreeNode* node = stack.back();stack.pop_back();v.push_back(node);if (node && node->right){stack.push_back(node->right);}if (node && node->left){stack.push_back(node->left);}}v.push_back(NULL);for(int i=0; i<v.size(); i++){if (v[i]){v[i]->left = NULL;v[i]->right = v[i+1];}}}};
C++ solution by pezy/LeetCode
#include <cstddef>struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:void flatten(TreeNode *root){for ( ; root; root = root->right)if (root->left) {TreeNode *end = root->left;while (end->right) end = end->right;end->right = root->right;root->right = root->left;root->left = NULL;}}};