Files
hello-algo/ru/codes/javascript/chapter_tree/binary_tree_dfs.js
Yudong Jin 772183705e Add ru version (#1865)
* Add Russian docs site baseline

* Add Russian localized codebase

* Polish Russian code wording

* Update ru code translation.

* Update code translation and chapter covers.

* Fix pythontutor extraction.

* Add README and landing page.

* placeholder of profiles

* Use figures of English version

* Remove chapter paperbook
2026-03-28 04:24:07 +08:00

61 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* File: binary_tree_dfs.js
* Created Time: 2022-12-04
* Author: IsChristina (christinaxia77@foxmail.com)
*/
const { arrToTree } = require('../modules/TreeNode');
const { printTree } = require('../modules/PrintUtil');
// Инициализировать список для хранения последовательности обхода
const list = [];
/* Предварительный обход */
function preOrder(root) {
if (root === null) return;
// Порядок обхода: корень -> левое поддерево -> правое поддерево
list.push(root.val);
preOrder(root.left);
preOrder(root.right);
}
/* Симметричный обход */
function inOrder(root) {
if (root === null) return;
// Порядок обхода: левое поддерево -> корень -> правое поддерево
inOrder(root.left);
list.push(root.val);
inOrder(root.right);
}
/* Обратный обход */
function postOrder(root) {
if (root === null) return;
// Порядок обхода: левое поддерево -> правое поддерево -> корень
postOrder(root.left);
postOrder(root.right);
list.push(root.val);
}
/* Driver Code */
/* Инициализация двоичного дерева */
// Здесь используется функция, напрямую строящая двоичное дерево из массива
const root = arrToTree([1, 2, 3, 4, 5, 6, 7]);
console.log('\nИнициализация двоичного дерева\n');
printTree(root);
/* Предварительный обход */
list.length = 0;
preOrder(root);
console.log('\nПоследовательность печати узлов при предварительном обходе = ' + list);
/* Симметричный обход */
list.length = 0;
inOrder(root);
console.log('\nПоследовательность печати узлов при симметричном обходе = ' + list);
/* Обратный обход */
list.length = 0;
postOrder(root);
console.log('\nПоследовательность печати узлов при обратном обходе = ' + list);