C++ recursion 3ms


  • 0
    X
    class Solution {
    public:
    	int re = 0;
    	int sumOfLeftLeaves(TreeNode* root) {
    		dfs(root,0);
    		return re;
    	}
    	void dfs(TreeNode *root,bool left) {
    		if (!root)
    			return;
    		if (left && !root->left && !root->right)
    			re += root->val;
    		dfs(root->left,1);
    		dfs(root->right,0);
    	}
    };

Log in to reply
 

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