代码拉取完成,页面将自动刷新
/*
* @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
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。