Divide and Conquer C++


  • 0
    S
    class Solution {
    public:
        TreeNode *flattenHelper(TreeNode *root) {
            if (NULL == root) {
                return NULL;
            }
            if (NULL == root->left && NULL == root->right) {
                return root;
            }
            if (root->left) {
                TreeNode *leftButtomNode = flattenHelper(root->left);
                leftButtomNode->right = root->right;
                root->right = root->left;
                root->left = NULL;
            }
            return flattenHelper(root->right);
        }
        
        void flatten(TreeNode* root) {
            flattenHelper(root);
        }
    };
    

Log in to reply
 

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