1 Star 0 Fork 0

孙启萌 / leetcode

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
404.左叶子之和.js 971 Bytes
一键复制 编辑 原始数据 按行查看 历史
孙启萌 提交于 2023-05-10 09:46 . 刷题
/*
* @lc app=leetcode.cn id=404 lang=javascript
*
* [404] 左叶子之和
*/
// @lc code=start
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var sumOfLeftLeaves = function(root) {
if (root.left === null && root.right === null) {
return 0
}
let result = 0
function next(root, type) {
if (root) {
if (root.left === null && root.right === null && type === 'left') {
// 如果改的节点是叶子节点 且 标记为 左节点
result += root.val
} else {
next(root.left, 'left')
next(root.right, 'right')
}
}
}
next(root, 'left')
return result
};
// @lc code=end
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sqm147896325/leetcode.git
git@gitee.com:sqm147896325/leetcode.git
sqm147896325
leetcode
leetcode
master

搜索帮助

344bd9b3 5694891 D2dac590 5694891