Easy solution in c++


  • 2
    N
    class Solution {
    public:
       	void flatten(TreeNode* root) {
    		if (root != NULL)
    			flat(root);
    	}
    
    	TreeNode* flat(TreeNode* root) {
    		TreeNode *left = root->left;
    		TreeNode *right = root->right;
    		TreeNode *tail = root;
    		if (left != NULL) {
     			tail->right = left;
    			tail->left = NULL;
    			tail =flat(left);
    		}
    		if (right != NULL) {
     			tail->right = right;
    			tail->left = NULL;
    			tail =flat(right);
    		}
    		return tail;
    
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.