Created
July 27, 2020 20:43
-
-
Save bparanj/0984ed417a8f72e978b450f4fbe58a82 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Definition for a binary tree node. | |
# class TreeNode | |
# attr_accessor :val, :left, :right | |
# def initialize(val = 0, left = nil, right = nil) | |
# @val = val | |
# @left = left | |
# @right = right | |
# end | |
# end | |
# @param {TreeNode} root | |
# @return {String[]} | |
def construct_paths(root, path, paths) | |
if root | |
path += root.val.to_s | |
if root.left.nil? && root.right.nil? | |
paths << path | |
else | |
path += '->' | |
construct_paths(root.left, path, paths) | |
construct_paths(root.right, path, paths) | |
end | |
end | |
end | |
def binary_tree_paths(root) | |
paths = [] | |
construct_paths(root, '', paths) | |
return paths | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment