Constant space by dfs concise solution


  • 0
    X
        void dfs(TreeNode* node,TreeNode* minL,TreeNode* maxR,int& res)
        {
            if(!node)
                return;
            dfs(node->left,minL,node,res);
            dfs(node->right,node,maxR,res);
            if(minL)
                res=min(res,abs(node->val-minL->val));
            if(maxR)
                res=min(res,abs(maxR->val-node->val));
        }
        
        int getMinimumDifference(TreeNode* root) 
        {
            int res=INT_MAX;
            dfs(root,NULL,NULL,res);
            return res;
        }
    

Log in to reply
 

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