Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class binaryTree {
- constructor(val) {
- this.val = val;
- this.left = null;
- this.right = null;
- }
- addLeft(val) {
- this.left = new binaryTree(val);
- return this.left;
- }
- addRight(val) {
- this.right = new binaryTree(val);
- return this.right;
- }
- }
- const hasPathToSum = (node, targetSum, targetLeft = targetSum) => {
- const currLeft = targetLeft - node.val;
- if (!node.left && !node.right) {
- if (currLeft === 0) {
- return true;
- }
- return false;
- }
- let leftPath = false;
- let rightPath = false;
- if (node.left && currLeft < node.val) {
- leftPath = hasPathToSum(node.left, targetSum, currLeft);
- }
- if (node.right && currLeft > node.val) {
- rightPath = hasPathToSum(node.right, targetSum, currLeft);
- }
- return false || leftPath || rightPath;
- };
Add Comment
Please, Sign In to add comment