0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PostOrder Traversal

0
Posted at

require 'test/unit'
require 'pry'
class PostOrderTraversal < Test::Unit::TestCase

  def test_run

    input = [1, nil, 2, 3]

    tree = TreeNode.new(1)
    tree.left = TreeNode.new(nil)
    tree.right = TreeNode.new(2)
    tree.right.left = TreeNode.new(3)
    assert_equal([3, 2, 1],
                 postorder_traversal(tree))

    assert_equal([],
                 postorder_traversal(nil))
  end

  def postorder_traversal(root, result = [])
    if root == nil || root.val.nil?
      return []
    end

    postorder_traversal(root.left, result) if root.left
    postorder_traversal(root.right, result) if root.right
    result << root.val
  end

end


# Definition for a binary tree node.
class TreeNode
  attr_accessor :val, :left, :right

  def initialize(val)
    @val = val
    @left, @right = nil, nil
  end
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?