前序、中序构建二叉树,并遍历

首先,构建BinaryTree.h头文件

//
// Created by JanzeeLiu on 2019-07-28.
//

#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iostream>
#include <vector>

struct TreeNode{
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int tempKey)
    {
        val=tempKey;
        left= nullptr;
        left= nullptr;
    }
};

class BinaryTree{
private:
    TreeNode* root;
    void helper(const TreeNode* root,std::vector<int> &result);
public:
    BinaryTree();
    BinaryTree(const std::vector<int> &preorder,const std::vector<int> &inorder);
    TreeNode* getroot();
    TreeNode* helper(const std::vector<int>& pre,int s1,int e1,const std::vector<int>& post,int s2,int e2);
    TreeNode* BuildTree(const std::vector<int> &preorder,const std::vector<int> &inorder);
    std::vector<int> preorder(const TreeNode* root);
//    void preorder(const TreeNode* tree);
//    std::vector<int> preorder(const TreeNode* root);
};
#endif //BINARYTREE_H

在编写BinaryTree.cpp文件来实现.h文件中的接口

//
// Created by JanzeeLiu on 2019-07-28.
//
#include <vector>
#include <iostream>
#include <stack>
#include "BinaryTree.h"
BinaryTree::BinaryTree() {
    root= nullptr;
}
BinaryTree::BinaryTree(const std::vector<int> &preorder,const std::vector<int> &inorder){
    std::cout<<"Construction"<<std::endl;
    root=BuildTree(preorder,inorder);
}
TreeNode* BinaryTree::getroot() {
    return this->root;
}

TreeNode* BinaryTree::BuildTree(const std::vector<int> &preorder,const std::vector<int> &inorder) {
    int s1=0,e1=preorder.size()-1;
    int s2=0,e2=inorder.size()-1;
    return helper(preorder,s1,e1,inorder,s2,e2);
}
TreeNode* BinaryTree::helper(const std::vector<int>& pre,int s1,int e1,const std::vector<int>& post,int s2,int e2){
    if(s1>e1 or s2>e2)
        return nullptr;
    auto* root=new TreeNode(pre[s1]);
    if(s1==e1)
        return root;
    int mid=pre[s1];
    int index=0;
    while(s2+index<=e2 && post[s2+index]!=mid)
        ++index;
    root->left=helper(pre,s1+1,s1+index,post,s2,s2+index-1);
    root->right=helper(pre,s1+1+index,e1,post,s2+1+index,e2);

    return root;
}


//void BinaryTree::preorder(const TreeNode* tree){
//    if(tree== nullptr)
//        return;
//    preorder(tree->left);
//    std::cout<<tree->val<<std::endl;
//    preorder(tree->right);
//}

std::vector<int> BinaryTree::preorder(const TreeNode* root) {
    std::vector<int> result;
    helper(root,result);
    return result;
}

void BinaryTree::helper(const TreeNode* root,std::vector<int> &result){
    if(root==nullptr)
        return;
    result.push_back(root->val);
    std::cout<<root->val<<std::endl;
    helper(root->left,result);
    helper(root->right,result);
}

//std::vector<int> BinaryTree::preorder(const TreeNode* root){
//    std::vector<int> res;
//    if(root == nullptr) return res;
//    std::stack<const TreeNode*> st;
//    const TreeNode* cur = root;
//     while(cur || !st.empty()){
//         while(cur){
//             res.push_back(cur->val);
//             st.push(cur);
//             cur = cur->left;
//         }
//         if(!st.empty()){
//             cur = st.top();
//             st.pop();
//             cur = cur->right;
//         }
//     }
//     return res;
//}

再编写main函数来使用一下

#include <iostream>
#include <vector>
#include "BinaryTree.h"
int main() {
    std::vector<int> preorder{3,9,20,15,7};
    std::vector<int> inorder{9,3,15,20,7};
    BinaryTree tree(preorder,inorder);
    std::vector<int> result;
    result=tree.preorder(tree.getroot());
    for(auto tmp:result)
        std::cout<<tmp<<std::endl;
    return 0;
}

正确输出应该是:3,9,20,15,7.

但是我的输出是:3,9

当helper函数这样写时,输出3,20,7

void BinaryTree::helper(const TreeNode* root,std::vector<int> &result){
    if(root==nullptr)
        return;
    result.push_back(root->val);
//    helper(root->left,result);
    std::cout<<root->val<<std::endl;
    helper(root->right,result);
}

说明树的构造是没有问题的,从结果看来,一遇到return,函数就返回了,而不是继续递归,真是奇怪,这是一个很常规的递归遍历方式,但是却不能输出正确结果,不知道是不是编译器的问题,我使用的是clion编译器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值