TheAlgorithms-Ruby/data_structures/binary_trees/inorder_traversal.rb

23 lines
498 B
Ruby
Raw Normal View History

2017-10-02 01:00:27 +02:00
# 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 {Integer[]}
def inorder_traversal(root)
ans = []
def traverse(node, ans)
if node != nil
traverse(node.left, ans)
ans.push(node.val)
traverse(node.right,ans)
end
end
traverse(root,ans)
return ans
end