Welcome to read the reference, if there are errors or questions, please leave a message to correct, thank you
Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \
3 4 6
The flattened tree should look like:
1
\
2
\
3
\
4
\
5
\
6
/** * Definition for binary tree * struct TreeNode {* int val; * TreeNode *left; * TreeNode *right; * Tre Enode (int x): Val (x), left (null), right (NULL) {} *}; */class Solution {public: treenode* flattenutil (TreeNode *root) { if (root==null ) return NULL; TreeNode *left = root->left; TreeNode *right = root->right; Root->left = NULL; if (left) { root->right = left; Root = Flattenutil (left); The root pointer points to left, that is, next } if (right) { root->right = right; root = Flattenutil (right); The root pointer points to right, next } return root ; void Flatten (TreeNode *root) { flattenutil (root); }};
Leetcode_114_flatten Binary Tree to Linked List