2017-10-02 01:00:27 +02:00
|
|
|
# Definition for a binary tree node.
|
|
|
|
# class TreeNode
|
2020-05-14 04:05:14 +02:00
|
|
|
# attr_accessor :val, :left, :right
|
|
|
|
# def initialize(val)
|
|
|
|
# @val = val
|
|
|
|
# @left, @right = nil, nil
|
|
|
|
# end
|
2017-10-02 01:00:27 +02:00
|
|
|
# end
|
|
|
|
|
|
|
|
# @param {TreeNode} root
|
|
|
|
# @return {Integer[]}
|
|
|
|
def inorder_traversal(root)
|
2020-05-14 04:05:14 +02:00
|
|
|
ans = []
|
|
|
|
def traverse(node, ans)
|
2021-02-07 08:05:54 +01:00
|
|
|
unless node.nil?
|
2020-05-14 04:05:14 +02:00
|
|
|
traverse(node.left, ans)
|
|
|
|
ans.push(node.val)
|
|
|
|
traverse(node.right, ans)
|
2017-10-02 01:00:27 +02:00
|
|
|
end
|
2020-05-14 04:05:14 +02:00
|
|
|
end
|
|
|
|
traverse(root, ans)
|
2021-02-07 08:05:54 +01:00
|
|
|
ans
|
|
|
|
end
|