This commit is contained in:
krahets 2024-04-19 19:50:50 +08:00
parent ea7cdae9a7
commit 2f5cc98102
2 changed files with 18 additions and 2 deletions

View File

@ -804,7 +804,15 @@ comments: true
=== "Ruby"
```ruby title="binary_tree_dfs.rb"
[class]{}-[func]{pre_order}
### 前序遍历 ###
def pre_order(root)
return if root.nil?
# 访问优先级:根节点 -> 左子树 -> 右子树
$res << root.val
pre_order(root.left)
pre_order(root.right)
end
### 中序遍历 ###
def in_order(root)

View File

@ -804,7 +804,15 @@ Depth-first search is usually implemented based on recursion:
=== "Ruby"
```ruby title="binary_tree_dfs.rb"
[class]{}-[func]{pre_order}
### 前序遍历 ###
def pre_order(root)
return if root.nil?
# 访问优先级:根节点 -> 左子树 -> 右子树
$res << root.val
pre_order(root.left)
pre_order(root.right)
end
### 中序遍历 ###
def in_order(root)