Last active
January 16, 2018 08:37
-
-
Save alexspark/5cbedb4ed8b13b4a9806d883b26e6422 to your computer and use it in GitHub Desktop.
Post-order traversal of a binary tree
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
class Node | |
attr_accessor :value, :left, :right | |
def initialize(value) | |
@value = value | |
end | |
end | |
def post_order_traversal(node) | |
post_order_traversal node.left if node.left | |
post_order_traversal node.right if node.right | |
puts node.value | |
end | |
a = Node.new(1) | |
a.left = Node.new(2) | |
a.left.left = Node.new(3) | |
a.left.right = Node.new(4) | |
a.right = Node.new(5) | |
post_order_traversal(a) # 3, 4, 2, 5, 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment