Sum of Left Leaves思路:智障题目
GitHub地址:https://github.com/corpsepiges/leetcode
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int sumOfLeftLeaves(TreeNode root) {
if (root==null) {
return 0;
}
return sum(root.left,true)+sum(root.right,false);
}
public int sum(TreeNode root,boolean flag){
if (root==null) {
return 0;
}
if (flag&&root.left==null&&root.right==null) {
return root.val;
}
return sum(root.left, true)+sum(root.right,false);
}
}
本文介绍了一个简单的二叉树遍历算法——左叶子之和。该算法通过递归方式计算二叉树中所有左叶子节点的值之和。文章提供了完整的Java实现代码,并附有清晰的GitHub链接供读者参考。

被折叠的 条评论
为什么被折叠?



