/**
* 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:
void flatten(TreeNode *root)
{
helper(root);
}
//输入根节点,将其拆成链表,并返回最后一个节点
TreeNode* helper(TreeNode *root){
if(root == NULL){
return NULL;
}
TreeNode* leftend = helper(root->left);
TreeNode* rightend = helper(root->right);
if(leftend != NULL){
leftend->right = root->right;
root->right = root->left;
root->left = NULL;
}
if(rightend != NULL){
return rightend;
}
if(leftend != NULL){
return leftend;
}
return root;
}
};
114. Flatten Binary Tree to Linked List
最新推荐文章于 2022-07-27 15:08:45 发布