We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
129. 求根节点到叶节点数字之和
The text was updated successfully, but these errors were encountered:
/* * @lc app=leetcode.cn id=129 lang=javascript * * [129] 求根节点到叶节点数字之和 */ // @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} (56 ms) */ var sumNumbers = function(root) { let sum = 0 const bst = (current, node) => { if(!node) return if(!node.left && !node.right) { sum += (current + node.val ) * 1; return } bst(current + node.val, node.left); bst(current + node.val, node.right); } bst('', root); return sum } // var sumNumbers = function(root) { // function traverse(node, num) { // if(!node) return null; // num += node.val // if(!node.left && !node.right) return +num; // return traverse(node.left, num) + traverse(node.right, num); // } // return traverse(root, ''); // }; // @lc code=end
Sorry, something went wrong.
No branches or pull requests
129. 求根节点到叶节点数字之和
The text was updated successfully, but these errors were encountered: