mirror of
https://github.com/krahets/hello-algo.git
synced 2026-08-20 15:30:58 +00:00
build
This commit is contained in:
@@ -804,7 +804,15 @@ comments: true
|
|||||||
=== "Ruby"
|
=== "Ruby"
|
||||||
|
|
||||||
```ruby title="binary_tree_dfs.rb"
|
```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)
|
def in_order(root)
|
||||||
|
|||||||
@@ -804,7 +804,15 @@ Depth-first search is usually implemented based on recursion:
|
|||||||
=== "Ruby"
|
=== "Ruby"
|
||||||
|
|
||||||
```ruby title="binary_tree_dfs.rb"
|
```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)
|
def in_order(root)
|
||||||
|
|||||||
Reference in New Issue
Block a user