题解 | #二叉树遍历#

二叉树遍历

https://www.nowcoder.com/practice/4b91205483694f449f94c179883c1fef

//二叉树的建立和二叉树的中序遍历
#include <iostream>
using namespace std;
struct TreeNode{
    char c;
    TreeNode* leftChild;
    TreeNode* rightChild;
};
TreeNode* Build(int& position, string str){
    char c=str[position++];
    if(c=='#') return NULL;
    TreeNode* root=(TreeNode*)malloc(sizeof(TreeNode));
    root->c=c;
    root->leftChild=Build(position,str);
    root->rightChild=Build(position,str);
    return root;
}
void inOrder(TreeNode* root){
    if(root==NULL) return;
    inOrder(root->leftChild);
    printf("%c ",root->c);
    inOrder(root->rightChild);
}
int main(){
    string str;
    while(cin>>str){
        int position=0;
        TreeNode* root = Build(position,str);
        inOrder(root);
    }
    return 0;
}

全部评论

相关推荐

用微笑面对困难:加急通知你不合适,也很吗有礼貌了你。
点赞 评论 收藏
分享
09-29 16:59
已编辑
门头沟学院 Java
牛客96609213...:疯狂背刺,之前还明确设置截止日期,还有笔试,现在一帮人卡在复筛,他反而一边开启扩招,还给扩招的免笔试,真服了,你好歹先把复筛中的给处理了再说
投递大疆等公司10个岗位
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务