{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "<h1>112. Path Sum</h1>\n", "<hr>\n", "\n", "<!--Copy Paste Leetcode statement between-->\n", "<p>Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.</p>\n", "\n", "<p><strong>Note:</strong> A leaf is a node with no children.</p>\n", "\n", "<p><strong>Example:</strong></p>\n", "\n", "<p>Given the below binary tree and <code>sum = 22</code>,</p>\n", "\n", "<pre> <strong>5</strong>\n", " <strong>/</strong> \\\n", " <strong>4</strong> 8\n", " <strong>/</strong> / \\\n", " <strong>11</strong> 13 4\n", " / <strong>\\</strong> \\\n", "7 <strong>2</strong> 1\n", "</pre>\n", "\n", "<p>return true, as there exist a root-to-leaf path <code>5->4->11->2</code> which sum is 22.</p>\n", "<!--Copy Paste Leetcode statement between-->\n", "\n", "<p> </p>\n", "<a href=\"https://leetcode.com/problems/path-sum/\">Source</a> \n", "<hr>" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# Definition for a binary tree node.\n", "class TreeNode:\n", " def __init__(self, val=0, left=None, right=None):\n", " self.val = val\n", " self.left = left\n", " self.right = right" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def has_path_sum(root, amount):\n", " \"\"\"Recursive approach.\"\"\"\n", " if root is None:\n", " return False\n", " if root.left is None and root.right is None:\n", " return root.val == amount\n", " if root.left is None: # Technically\n", " return has_path_sum(root.right, amount-root.val) # this part is not needed\n", " if root.right is None: # but \n", " return has_path_sum(root.left, amount-root.val) # speed up the algo a tiny bit\n", " return (has_path_sum(root.left, amount-root.val) or has_path_sum(root.right, amount-root.val))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "<hr>\n", "<h4>Follow up:</h4>\n", "<p>Solve it both recursively and iteratively.</p>\n", "\n", "<h4>Code</h4>" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "def has_path_sum(root, amount):\n", " \"\"\"Iterative approach using BFS traversal with a list.\"\"\"\n", " if not root:\n", " return False\n", " queue = [(root, 0)]\n", " for (node, prev_sum) in queue:\n", " if not node:\n", " continue\n", " curr_sum = prev_sum + node.val\n", " if not node.left and not node.right and curr_sum == amount: # only check for leaf nodes\n", " return True\n", " queue.extend([(node.left, curr_sum), (node.right, curr_sum)])\n", " return False" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "def has_path_sum(root, amount):\n", " \"\"\"Iterative approach using BFS traversal with a deque (slower).\"\"\"\n", " if not root:\n", " return 0\n", " queue = deque()\n", " queue.append((root, 0))\n", " while queue:\n", " node, prev_sum = queue.popleft()\n", " if not node:\n", " continue\n", " curr_sum = prev_sum + node.val\n", " if not node.left and not node.right and curr_sum == amount: # only checkfor leaf nodes\n", " return True\n", " queue.extend([(node.left, curr_sum), (node.right, curr_sum)])\n", " return False" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.2" } }, "nbformat": 4, "nbformat_minor": 1 }