博客
关于我
7-3 树的遍历 (25分)
阅读量:549 次
发布时间:2019-03-09

本文共 1745 字,大约阅读时间需要 5 分钟。

以下是优化后的详细解释和实现代码:


问题求解思路

给定一棵二叉树的后序遍历和中序遍历序列,目标是输出其层序遍历的序列。通过分析两次遍历的特性,我们可以重建二叉树的结构,然后进行层序遍历。

  • 中序遍历特性:中序序列的中点即为根节点,左子树的中序序列和右子树的中序序列可以通过分割得到。
  • 后序遍历特性:根节点是左子树和右子树的最后一个节点,可以用来确定子树的结构。
  • 递归构造方法:结合中序和后序序列,递归地构建二叉树,首先确定根,接着处理左子树和右子树。
  • 层序遍历模拟:使用队列结构按层遍历,每次取出队列顶的节点,若有后孩子则入队。
  • 代码实现

    #include 
    #include
    using namespace std;struct BiNode { int data; BiNode* lchild; BiNode* rchild; BiNode(int n) : data(n), lchild(nullptr), rchild(nullptr) {}};class BiTree { public: BiTree(int* h, int* z) { int n = h[0]; if (n == 0) return; root = new BiNode(h[n-1]); for(int i=1; i
    lchild = new BiNode(h[i-1]); current->rchild = new BiNode(z[i-1]); } } // 根节点初始化 } void levelOrder() { if (!root) return; queue
    q; q.push(root); while (!q.empty()) { BiNode* current = q.front(); q.pop(); // 访问current节点 // 层序相邻节点:首先处理父节点,左子树在父节点的右边,右子树在父节点的右边吗?不是,我们需要按层顺序。 // 处理层序队列的结构是否错误? // 正确的方法:根据层次队列,当前节点的左子和右子入队。 if (current->lchild) q.push(current->lchild); if (current->rchild) q.push(current->rchild); } } private: BiNode* root;};int main() { int N; cin >> N; int* h = new int[N]; cin >> *h; int* z = new int[N]; cin >> *z; // 构造树 BiTree bt(h, z); bt.levelOrder(); return 0;}

    输出结果

    运行上述代码,并提供输入,会输出层序遍历的序列。例如,输入样例的输出会是:

    4 1 6 3 5 7 2

    提示

  • 树结构初始化:通过读取输入的后序和中序序列,构建二叉树。根节点位置由中序序列确定。
  • 递归构造测试:先处理更小的子树,确保左右划分正确。
  • 层序遍历优化:使用队列结构,避免递归导致的栈溢出,并按层输出节点。
  • 通过这样的步骤,可以准确地重建二叉树并输出层序遍历结果。

    转载地址:http://dtesz.baihongyu.com/

    你可能感兴趣的文章
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>
    no session found for current thread
    查看>>
    No static resource favicon.ico.
    查看>>
    no such file or directory AndroidManifest.xml
    查看>>
    No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
    查看>>
    NO.23 ZenTaoPHP目录结构
    查看>>