ð 404. å·Šå¶åä¹å
2022幎10æ10æ¥
- algorithm
ð 404. å·Šå¶åä¹å
éŸåºŠ: ð
é®é¢æè¿°
解æ³
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
int res = 0;
public int sumOfLeftLeaves(TreeNode root) {
// æè·¯ïŒ
// éåœ
mySol(root.left, true);
mySol(root.right, false);
return res;
}
private void mySol(TreeNode root, boolean exp) {
// éåœç»æ¢æ¡ä»¶
if(root == null) {
return;
}
if(root.left == null && root.right == null) {
// 诎ææ¯å¶åèç¹ïŒå€ææ¯åŠæ¯å·Šå¶å
if(exp) {
res += root.val;
}
return;
}
mySol(root.left, true);
mySol(root.right, false);
}
}