java - 将订单代码转换为预购和后购
问题描述
我如何把它变成一个前序和后序遍历?此代码仅以中序样式遍历树。
void inorder() {
inorderRec(root);
}
// Inorder Traversal
void inorderRec(Node root) {
if (root != null) {
inorderRec(root.left);
System.out.print(root.key + " -> ");
inorderRec(root.right);
}
}
解决方案
您可以更改语句的顺序:
void preOrderRec(Node root) {
if (root != null) {
System.out.print(root.key + " -> ");
inorderRec(root.left);
inorderRec(root.right);
}
void postOrderRec(Node root) {
if (root != null) {
inorderRec(root.left);
inorderRec(root.right);
System.out.print(root.key + " -> ");
}
推荐阅读
- android - 谷歌登录api无法使用firebase运行android studio
- c# - Blazor 服务器端 - 使用 StateHasChanged 循环实时更新
- javascript - JS将两个数字相加并显示到小数点后两位
- android - 如何在 login.vue 中添加标题?
- c - C使用strtok()将字符串拆分为字符串数组,没有NULL作为最后一个元素
- java - java中的进程间文件锁定引发异常
- sql - 将时间戳转换为日期时如何摆脱 00:00:00?
- scala - 如果精度全为零,则精度仅在 scala 中设置为小数点后 1 位
- xml - 为什么 XSLT 文档被认为是“样式表”?
- vb6 - VB6 中的报告无法从远程桌面工作