Update binary_tree_bfs codes

pull/381/head
krahets 2 years ago
parent 8e0080f003
commit 7238c560d1

@ -7,7 +7,7 @@
#include "../include/include.hpp"
/* 层序遍历 */
vector<int> hierOrder(TreeNode* root) {
vector<int> levelOrder(TreeNode* root) {
// 初始化队列,加入根结点
queue<TreeNode*> queue;
queue.push(root);
@ -35,7 +35,7 @@ int main() {
PrintUtil::printTree(root);
/* 层序遍历 */
vector<int> vec = hierOrder(root);
vector<int> vec = levelOrder(root);
cout << endl << "层序遍历的结点打印序列 = ";
PrintUtil::printVector(vec);

@ -13,7 +13,7 @@ public class binary_tree_bfs
{
/* 层序遍历 */
public List<int> hierOrder(TreeNode root)
public List<int> levelOrder(TreeNode root)
{
// 初始化队列,加入根结点
Queue<TreeNode> queue = new();
@ -41,7 +41,7 @@ public class binary_tree_bfs
Console.WriteLine("\n初始化二叉树\n");
PrintUtil.PrintTree(root);
List<int> list = hierOrder(root);
List<int> list = levelOrder(root);
Console.WriteLine("\n层序遍历的结点打印序列 = " + string.Join(",", list.ToArray()));
}
}

@ -11,7 +11,7 @@ import (
)
/* 层序遍历 */
func hierOrder(root *TreeNode) []int {
func levelOrder(root *TreeNode) []int {
// 初始化队列,加入根结点
queue := list.New()
queue.PushBack(root)

@ -11,7 +11,7 @@ import (
. "github.com/krahets/hello-algo/pkg"
)
func TestHierOrder(t *testing.T) {
func TestLevelOrder(t *testing.T) {
/* 初始化二叉树 */
// 这里借助了一个从数组直接生成二叉树的函数
root := ArrToTree([]any{1, 2, 3, 4, 5, 6, 7})
@ -19,6 +19,6 @@ func TestHierOrder(t *testing.T) {
PrintTree(root)
// 层序遍历
nums := hierOrder(root)
nums := levelOrder(root)
fmt.Println("\n层序遍历的结点打印序列 =", nums)
}

@ -11,7 +11,7 @@ import java.util.*;
public class binary_tree_bfs {
/* 层序遍历 */
static List<Integer> hierOrder(TreeNode root) {
static List<Integer> levelOrder(TreeNode root) {
// 初始化队列,加入根结点
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
// 初始化一个列表,用于保存遍历序列
@ -35,7 +35,7 @@ public class binary_tree_bfs {
PrintUtil.printTree(root);
/* 层序遍历 */
List<Integer> list = hierOrder(root);
List<Integer> list = levelOrder(root);
System.out.println("\n层序遍历的结点打印序列 = " + list);
}
}

@ -8,7 +8,7 @@ const { arrToTree } = require("../include/TreeNode");
const { printTree } = require("../include/PrintUtil");
/* 层序遍历 */
function hierOrder(root) {
function levelOrder(root) {
// 初始化队列,加入根结点
let queue = [root];
// 初始化一个列表,用于保存遍历序列
@ -33,5 +33,5 @@ console.log("\n初始化二叉树\n");
printTree(root);
/* 层序遍历 */
let list = hierOrder(root);
let list = levelOrder(root);
console.log("\n层序遍历的结点打印序列 = " + list);

@ -10,7 +10,7 @@ from include import *
""" 层序遍历 """
def hier_order(root: Optional[TreeNode]):
def level_order(root: Optional[TreeNode]):
# 初始化队列,加入根结点
queue = collections.deque()
queue.append(root)
@ -35,6 +35,6 @@ if __name__ == "__main__":
print_tree(root)
# 层序遍历
res = hier_order(root)
res = level_order(root)
print("\n层序遍历的结点打印序列 = ", res)
assert res == [1, 2, 3, 4, 5, 6, 7]

@ -7,7 +7,7 @@
import utils
/* */
func hierOrder(root: TreeNode) -> [Int] {
func levelOrder(root: TreeNode) -> [Int] {
//
var queue: [TreeNode] = [root]
//
@ -36,7 +36,7 @@ enum BinaryTreeBFS {
PrintUtil.printTree(root: node)
/* */
let list = hierOrder(root: node)
let list = levelOrder(root: node)
print("\n层序遍历的结点打印序列 = \(list)")
}
}

@ -9,7 +9,7 @@ import { arrToTree } from '../module/TreeNode';
import { printTree } from '../module/PrintUtil';
/* 层序遍历 */
function hierOrder(root: TreeNode | null): number[] {
function levelOrder(root: TreeNode | null): number[] {
// 初始化队列,加入根结点
const queue = [root];
// 初始化一个列表,用于保存遍历序列
@ -35,7 +35,7 @@ console.log('\n初始化二叉树\n');
printTree(root);
/* 层序遍历 */
const list = hierOrder(root);
const list = levelOrder(root);
console.log('\n层序遍历的结点打印序列 = ' + list);
export {};

@ -6,7 +6,7 @@ const std = @import("std");
const inc = @import("include");
//
fn hierOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {
fn levelOrder(comptime T: type, mem_allocator: std.mem.Allocator, root: *inc.TreeNode(T)) !std.ArrayList(T) {
//
const L = std.TailQueue(*inc.TreeNode(T));
var queue = L{};
@ -48,7 +48,7 @@ pub fn main() !void {
try inc.PrintUtil.printTree(root, null, false);
//
var list = try hierOrder(i32, mem_allocator, root.?);
var list = try levelOrder(i32, mem_allocator, root.?);
defer list.deinit();
std.debug.print("\n层序遍历的结点打印序列 = ", .{});
inc.PrintUtil.printList(i32, list);

Loading…
Cancel
Save