GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode-javascript/112 Path Sum.js at master · chihungyu1116/leetcode-javascript Expand file tree/ Copy path112 Path Sum.jsMore file actions 54 lines (48 loc) · 1.15 KB / Copy path112 Path Sum.jsFile metadata and controls54 lines (48 loc) · 1.15 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 // Leetcode #112 // Language: Javascript // Problem: https://leetcode.com/problems/path-sum/ // Author: Chihung Yu /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @param {number} sum * @return {boolean} */ var hasPathSum = function(root, sum) { if(root === null){ return false; } if(root.val === sum && root.left === null && root.right === null){ return true; } return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val); }; /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.left = this.right = null; * } */ /** * @param {TreeNode} root * @param {number} sum * @return {boolean} */ var hasPathSum = function(root, sum) { if (root === null) { return false; } var left = root.left; var right = root.right; if (left === null && right === null) { return root.val === sum; } return hasPathSum(left, sum - root.val) || hasPathSum(right, sum - root.val); }; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |