二叉树遍历 A1102. Invert a Binary Tree (25) 反转二叉树 并输出层序和中序遍历 (使用静态二叉树)

#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <queue>
using namespace std;
const int maxn = 110;
struct node{
    int lchild,rchild;
}Node[maxn];
bool notRoot[maxn] = {false};//记录是否不是根节点,初始均是根节点
int n,num = 0;//n为节点个数,num为当前已经输出的节点个数
//print函数输出节点id的编号
void print(int id){
    printf("%d",id);
    num++;
    if(num < n)printf(" ");
    else printf("\n");
}
//中序遍历
void inOrder(int root){
    if(root == -1){
        return;
    }
    inOrder(Node[root].lchild);
    print(root);
    inOrder(Node[root].rchild);
}
//层序遍历
void BFS(int root){
    queue<int> q;
    q.push(root);
    while(!q.empty()){
        int now = q.front();
        q.pop();
        print(now);
        if(Node[now].lchild != -1) q.push(Node[now].lchild);
        if(Node[now].rchild != -1) q.push(Node[now].rchild);
    }
}
//后序遍历
void postOrder(int root){
    if(root == -1){
        return;
    }
    postOrder(Node[root].lchild);
    postOrder(Node[root].rchild);
    swap(Node[root].lchild,Node[root].rchild);
}
int strToNum(char c){
    if(c == ‘-‘){
        return -1;
    }
    else{
        notRoot[c-‘0‘] = true;
        return c- ‘0‘;//返回节点编号
    }
}
int findRoot(){
    for(int i =0;i<n;++i){
        if(notRoot[i] == false){
            return i;
        }
    }
}
int main(){
    char lchild,rchild;
    scanf("%d",&n);
    for(int i=0;i<n;++i){
        scanf("%*c%c %c",&lchild,&rchild);
        Node[i].lchild = strToNum(lchild);
        Node[i].rchild = strToNum(rchild);
    }
    int root = findRoot();
    postOrder(root);
    BFS(root);
    num = 0;
    inOrder(root);
    system("pause");
    return 0;
}

原文地址:https://www.cnblogs.com/JasonPeng1/p/12232351.html

时间: 2024-08-08 07:10:45

二叉树遍历 A1102. Invert a Binary Tree (25) 反转二叉树 并输出层序和中序遍历 (使用静态二叉树)的相关文章

1102. Invert a Binary Tree (25)【二叉树】——PAT (Advanced Level) Practise

题目信息 1102. Invert a Binary Tree (25) 时间限制400 ms 内存限制65536 kB 代码长度限制16000 B The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off.

leetcode题解:Construct Binary Tree from Inorder and Postorder Traversal(根据中序和后序遍历构造二叉树)

题目: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 说明: 1)实现与根据先序和中序遍历构造二叉树相似,题目参考请进 算法思想 中序序列:C.B.E.D.F.A.H.G.J.I 后序序列:C.E.F.D.B.H.J.I.G.A 递归思路: 根据后序遍历的特点,知道后序

PAT Advanced 1102 Invert a Binary Tree (25分)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off. Now it's your turn to prove that YOU CAN invert a binary tree! Input Specif

1102. Invert a Binary Tree (25)

The following is from Max Howell @twitter: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a whiteboard so fuck off. Now it's your turn to prove that YOU CAN invert a binary tree! Input Specif

LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal (用中序和后序树遍历来建立二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. 题目标签:Array, Tree 这到题目和105 几乎是一摸一样的,唯一的区别就是把pre-order 换成 post-order.因为post-order是最后一个数字是root,所以要从右向左的遍历.还需要把helpe

PAT (Advanced Level) 1102. Invert a Binary Tree (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<queue> #include<stack> #include<algorithm> using namespace std; struct Node { int left; int right; }s[20]; int

leetCode 105.Construct Binary Tree from Preorder and Inorder Traversal (根据前序遍历和中序遍历构造二叉树)

Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:首先根据前序遍历得到根节点,然后在中序遍历中得到根节点的位置,左边的为左子树,右边的为右子树. 然后再递归求解左子树和右子树的构造即可.代码如下: /** * Definition for a binary tree

leetCode 106.Construct Binary Tree from Inorder and Postorder Traversal (根据中序遍历和后序遍历构造二叉树)

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. 思路:这题和上题类似,前序第一个是根节点,后序遍历最后一个是根节点.其余步骤类似. 代码如下: /** * Definition for a binary tree node. * public class TreeNod

题目:输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树

问题描述: 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回. 思路: 在二叉树的前序遍历序列中,第一个数字总是树的根结点的值.但在中序遍历序列中,根结点的值在序列的中间,左子树的结点的值位于根结点的值的左边,而右子树的结点的值位于根结点的值的右边.因此我们需要扫描中序遍历序列,才能找到根结点的值. 如下图所示,