-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbinary_tree_paths.rb
54 lines (47 loc) · 1.02 KB
/
binary_tree_paths.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# https://leetcode.com/problems/binary-tree-paths/
#
# Given a binary tree, return all root-to-leaf paths.
#
# For example:
#
# Given the following binary tree:
#
# 1
# / \
# 2 3
# \
# 5
#
# All root-to-leaf paths are:
#
# ["1->2->5", "1->3"]
#
# Credits:
#
# Special thanks to @jianchao.li.fighter for adding this problem and
# creating all test cases.
# Definition for a binary tree node.
# class TreeNode
# attr_accessor :val, :left, :right
# def initialize(val)
# @val = val
# @left, @right = nil, nil
# end
# end
# @param {TreeNode} root
# @return {String[]}
def binary_tree_paths(root)
return [] if root.nil?
return [].tap { |paths| _traversal_(root, [], paths) }
end
private def _traversal_(root, tracing, paths)
tracing.push(root)
l, r = root.left, root.right
if l.nil? && r.nil?
paths << tracing.map(&:val).join('->')
else
_traversal_(l, tracing, paths) if l
_traversal_(r, tracing, paths) if r
end
tracing.pop
end