-
[LeetCode] 404. Sum of Left LeavesLeetCode 2021. 11. 4. 11:56728x90
https://leetcode.com/problems/sum-of-left-leaves/
Sum of Left Leaves - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
Sum of Left Leaves - Easy

왼쪽 자식 중 leaf 노드 인 것들의 val만 더하는 문제다.
쭉 훑어주면서 leaf 인지 체크와 left 자식 노드인지 체크만 해주면 된다.
전체 풀이 코드
class Solution { public: int ret = 0; int sumOfLeftLeaves(TreeNode* root) { run(root,false); return ret; } void run(TreeNode* node, bool left) { if (!node->left && !node->right && left) ret+=node->val; if (node->left) run(node->left, true); if (node->right) run(node->right, false); } };'LeetCode' 카테고리의 다른 글
[LeetCode] 764. Largest Plus Sign (0) 2021.11.04 [LeetCode] 1346. Check If N and Its Double Exist (0) 2021.11.04 [LeetCode] 129. Sum Root to Leaf Numbers (0) 2021.11.03 [LeetCode] 1481. Least Number of Unique Integers after K Removals (0) 2021.11.01 [LeetCode] 1047. Remove All Adjacent Duplicates In String (0) 2021.11.01