The title comes from Leetcode
https://leetcode.com/problems/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
Click to show hints.
Hide TagsTree Depth-first SearchHas you met this question in a real interview?
/** * 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: void Flatten (TreeNode *root) { if (root==null) return; if (root->left!=null) { TreeNode *tmp=root->right; TreeNode *cur=root->left; root->right=root->left; while (cur->right!=null) cur=cur->right; cur->right=tmp; root->left=null; } Flatten (root->right); }};
Flatten Binary Tree to Linked List total accepted:45093 Total submissions:156588