{ "cells": [ { "cell_type": "markdown", "source": [ "Given two binary trees, write a function to check if they are the same or not.\n", "\n", "Two binary trees are considered the same if they are structurally identical\n", "and the nodes have the same value.\n", "\n", "**Example 1:**\n", "\n", "\n", "\n", " Input: 1 1\n", " / \\ / \\\n", " 2 3 2 3\n", "\n", " [1,2,3], [1,2,3]\n", "\n", " Output: true\n", "\n", "\n", "**Example 2:**\n", "\n", "\n", "\n", " Input: 1 1\n", " / \\\n", " 2 2\n", "\n", " [1,2], [1,null,2]\n", "\n", " Output: false\n", "\n", "\n", "**Example 3:**\n", "\n", "\n", "\n", " Input: 1 1\n", " / \\ / \\\n", " 2 1 1 2\n", "\n", " [1,2,1], [1,1,2]\n", "\n", " Output: false" ], "metadata": {} }, { "outputs": [ { "output_type": "execute_result", "data": { "text/plain": "is_same_tree (generic function with 1 method)" }, "metadata": {}, "execution_count": 1 } ], "cell_type": "code", "source": [ "# @lc code=start\n", "using LeetCode\n", "\n", "function is_same_tree(p::Union{TreeNode, Nothing}, q::Union{TreeNode, Nothing})::Bool\n", " if isnothing(p) && isnothing(q)\n", " return true\n", " elseif isnothing(p) || isnothing(q)\n", " return false\n", " elseif p.val != q.val\n", " return false\n", " else\n", " return is_same_tree(p.left, p.left) && is_same_tree(p.right, p.right)\n", " end\n", "end\n", "\n", "# @lc code=end" ], "metadata": {}, "execution_count": 1 }, { "cell_type": "markdown", "source": [ "---\n", "\n", "*This notebook was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).*" ], "metadata": {} } ], "nbformat_minor": 3, "metadata": { "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.10.1" }, "kernelspec": { "name": "julia-1.10", "display_name": "Julia 1.10.1", "language": "julia" } }, "nbformat": 4 }