hello-algo/zh-hant/codes/ruby/chapter_tree/binary_tree_dfs.rb
Yudong Jin f616dac7da
Bug fixes and improvements (#1298)
* Fix is_empty() implementation in the stack and queue chapter

* Update en/CONTRIBUTING.md

* Remove "剩余" from the state definition of knapsack problem

* Sync zh and zh-hant versions

* Update the stylesheets of code tabs

* Fix quick_sort.rb

* Fix TS code

* Update chapter_paperbook

* Upload the manuscript of 0.1 section

* Fix binary_tree_dfs.rb

* Bug fixes

* Update README

* Update README

* Update README

* Update README.md

* Update README

* Sync zh and zh-hant versions

* Bug fixes
2024-04-22 02:26:32 +08:00

62 lines
1.3 KiB
Ruby

=begin
File: binary_tree_dfs.rb
Created Time: 2024-04-18
Author: Xuan Khoa Tu Nguyen (ngxktuzkai2000@gmail.com)
=end
require_relative '../utils/tree_node'
require_relative '../utils/print_util'
### 前序走訪 ###
def pre_order(root)
return if root.nil?
# 訪問優先順序:根節點 -> 左子樹 -> 右子樹
$res << root.val
pre_order(root.left)
pre_order(root.right)
end
### 中序走訪 ###
def in_order(root)
return if root.nil?
# 訪問優先順序:左子樹 -> 根節點 -> 右子樹
in_order(root.left)
$res << root.val
in_order(root.right)
end
### 後序走訪 ###
def post_order(root)
return if root.nil?
# 訪問優先順序:左子樹 -> 右子樹 -> 根節點
post_order(root.left)
post_order(root.right)
$res << root.val
end
### Driver Code ###
if __FILE__ == $0
# 初始化二元樹
# 這裡藉助了一個從陣列直接生成二元樹的函式
root = arr_to_tree([1, 2, 3, 4, 5, 6, 7])
puts "\n初始化二元樹\n\n"
print_tree(root)
# 前序走訪
$res = []
pre_order(root)
puts "\n前序走訪的節點列印序列 = #{$res}"
# 中序走訪
$res.clear
in_order(root)
puts "\nn中序走訪的節點列印序列 = #{$res}"
# 後序走訪
$res.clear
post_order(root)
puts "\nn後序走訪的節點列印序列 = #{$res}"
end