2 lines of C++ solution


  • 0
    W

    /**

    • Definition for a binary tree node.
    • struct TreeNode {
    • int val;
      
    • TreeNode *left;
      
    • TreeNode *right;
      
    • TreeNode(int x) : val(x), left(NULL), right(NULL) {}
      
    • };
      /
      class Solution {
      public:
      int sumOfLeftLeaves(TreeNode
      root) {
      if(!root) return 0;
      return (root->left&&!root->left->left&&!root->left->right)? root->left->val+sumOfLeftLeaves(root->right):sumOfLeftLeaves(root->left)+sumOfLeftLeaves(root->right);
      }
      };

Log in to reply
 

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