{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "

112. Path Sum

\n", "
\n", "\n", "\n", "

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.

\n", "\n", "

Note: A leaf is a node with no children.

\n", "\n", "

Example:

\n", "\n", "

Given the below binary tree and sum = 22,

\n", "\n", "
      5\n",
    "     / \\\n",
    "    4   8\n",
    "   /   / \\\n",
    "  11  13  4\n",
    " /  \\      \\\n",
    "7    2      1\n",
    "
\n", "\n", "

return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

\n", "\n", "\n", "

 

\n", "Source \n", "
" ] }, { "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": [ "

Code

" ] }, { "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": [ "
\n", "

Follow up:

\n", "

Solve it both recursively and iteratively.

\n", "\n", "

Code

" ] }, { "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 }