Was wrong with my code?


  • 0
    X
    class Solution {
    public:
    	void dfs(TreeNode *&p,int l,int r,vector<int> &num){
    		if(l>r){p=NULL; return;}
    		p->val=num[(l+r)/2];
    		TreeNode *left,*right;
    		p->left=left;
    		p->right=right;
    		dfs(left,l,(l+r)/2-1,num);
    		dfs(right,(l+r)/2+1,r,num);
    	}
        TreeNode *sortedArrayToBST(vector<int> &num) {
    		TreeNode *head;
    		dfs(head,0,num.size()-1,num);
    		return head;
            
        }
    };
    

    Runtime Error, why?


  • 0
    F

    TreeNode &*p rather than TreeNode *&p?


Log in to reply
 

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