0

我的代码是:

class Tree
  attr_accessor :node, :left, :right

  def initialize(*args)
    self.node = args[0]
    self.left = args[1]
    self.right = args[2]
  end

  def inorder
    if self.left
      self.left.inorder
    else
      return
    end
    puts self.node
    if self.right
      self.right.inorder
    else
      return
    end
  end
end

t2 = Tree.new 2
t1 = Tree.new 1
t  = Tree.new 3,t1,t2
t.inorder

仅打印根节点 3。这里出了什么问题?

4

1 回答 1

3

因为你的代码会returnputs.

  def inorder
    if self.left
      self.left.inorder
    end
    puts self.node
    if self.right
      self.right.inorder
    end
  end
于 2012-06-04T04:23:54.780 回答