0404.cpp (566B)
1 class Solution { 2 public: 3 int sumOfLeftLeaves(TreeNode *root) { 4 if (!root) return 0; 5 6 int res = 0; 7 stack<TreeNode *> st; 8 9 st.push(root); 10 while (!st.empty()) { 11 TreeNode *root = st.top(); 12 st.pop(); 13 if (root->left) { 14 if (!root->left->left && !root->left->right) 15 res += root->left->val; 16 else 17 st.push(root->left); 18 } 19 if (root->right) st.push(root->right); 20 } 21 return res; 22 } 23 };