Skip to content

[김한기] Week 3 solutions #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions climbing-stairs/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
defmodule Solution do
@spec climb_stairs(n :: integer) :: integer
def climb_stairs(n) do
do_climb_stairs(n, 1, 0, 1)
end

defp do_climb_stairs(n, n, n_1, n_2), do: n_1 + n_2

defp do_climb_stairs(n, step, n_1, n_2) do
do_climb_stairs(n, step + 1, n_2, n_1 + n_2)
end
end
19 changes: 19 additions & 0 deletions maximum-depth-of-binary-tree/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Definition for a binary tree node.
#
# defmodule TreeNode do
# @type t :: %__MODULE__{
# val: integer,
# left: TreeNode.t() | nil,
# right: TreeNode.t() | nil
# }
# defstruct val: 0, left: nil, right: nil
# end

defmodule Solution do
@spec max_depth(root :: TreeNode.t | nil) :: integer
def max_depth(nil, max_depth), do: max_depth

def max_depth(%TreeNode{left: left, right: right}, max_depth \\ 0) do
Enum.max([max_depth(left, max_depth + 1), max_depth(right, max_depth + 1)])
end
end
29 changes: 29 additions & 0 deletions same-tree/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Definition for a binary tree node.
#
# defmodule TreeNode do
# @type t :: %__MODULE__{
# val: integer,
# left: TreeNode.t() | nil,
# right: TreeNode.t() | nil
# }
# defstruct val: 0, left: nil, right: nil
# end
#
# Super simple solution
# defmodule Solution do
# @spec is_same_tree(p :: TreeNode.t | nil, q :: TreeNode.t | nil) :: boolean
# def is_same_tree(p, q) do
# p == q
# end
# end

defmodule Solution do
@spec is_same_tree(p :: TreeNode.t | nil, q :: TreeNode.t | nil) :: boolean
def is_same_tree(nil, nil), do: true
def is_same_tree(_, nil), do: false
def is_same_tree(nil, _), do: false

def is_same_tree(p, q) do
p.val == q.val && is_same_tree(p.left, q.left) && is_same_tree(p.right, q.right)
end
end
21 changes: 21 additions & 0 deletions subtree-of-another-tree/han.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Definition for a binary tree node.
#
# defmodule TreeNode do
# @type t :: %__MODULE__{
# val: integer,
# left: TreeNode.t() | nil,
# right: TreeNode.t() | nil
# }
# defstruct val: 0, left: nil, right: nil
# end

defmodule Solution do
@spec is_subtree(root :: TreeNode.t | nil, sub_root :: TreeNode.t | nil) :: boolean
def is_subtree(root, root), do: true

def is_subtree(%TreeNode{left: left, right: right}, sub_root) do
is_subtree(left, sub_root) || is_subtree(right, sub_root)
end

def is_subtree(_, _), do: false
end