114. Flatten Binary Tree to Linked List(M)

时间:2023-03-09 13:32:35
114. Flatten Binary Tree to Linked List(M)
. Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place. For example, given the following tree: / \ / \ \ The flattened tree should look like: \ \ \ \ \
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* prev = NULL;
void flatten(TreeNode* root) {
//using this solution: https://leetcode.com/problems/flatten-binary-tree-to-linked-list/discuss/36977/My-short-post-order-traversal-Java-solution-for-share
if(root == NULL)
return;
flatten(root->right);
flatten(root->left);
root->left = NULL;
root->right = prev;
prev = root; }
};

114. Flatten Binary Tree to Linked List(M)