树的三种遍历递归与非递归法和叶子分支节点高度


树不写递归遍历是真的难写!!!

6-3 二叉树的非递归遍历 (25 分)

void InorderTraversal( BinTree BT )
{
    Stack s = CreateStack();  
    

    while(!IsEmpty(s) || BT){
        if(BT){
            Push(s,BT);
            BT = BT->Left;
        }else{
            BT = Peek(s);
            Pop(s);
            printf(" %c",BT->Data);
            BT = BT->Right;
        }
    }
    return;
}

void PreorderTraversal( BinTree BT )
{
    
	Stack s = CreateStack();
	if(BT == NULL) return;
	
	Push(s,BT);
	
	while(!IsEmpty(s)){
		BT = Peek(s);
        Pop(s);
		if(BT->Right) Push(s,BT->Right);
		if(BT->Left) Push(s,BT->Left);
        
		printf(" %c",BT->Data);
	}
}

void PostorderTraversal( BinTree BT ){
    
    
    Stack s = CreateStack();
    
    BinTree q = NULL;
    
    while(!IsEmpty(s)||BT){
        if(BT){
            Push(s,BT);
            BT = BT->Left;
        }else{
            BT = Peek(s);
            if(BT->Right && BT->Right != q) BT = BT->Right;
            else{
                Pop(s);
                printf(" %c",BT->Data);
                q = BT;
                BT = NULL;
            }
        }
        
    }
    return;
}

6-2 是否二叉搜索树 (25 分)

bool IsBST ( BinTree T )
{
    if(T == NULL) return true;
    if(T->Left && T->Right)
    {
        
        BinTree a = T->Left , b = T->Right;
        
        while(a->Right) a = a->Right;
        while(b->Left) b = b->Left;
        
        if(a->Data >= T->Data || b->Data <= T->Data || T->Left->Data >= T->Data || T->Data >= T->Right->Data){
            return false;
        }
        
        
    }
    return IsBST(T->Left) && IsBST(T->Right);
}

6-7 二叉树的分支结点数* (20 分)

int NumBranch(const NODE *root)
{
    if(root == NULL) return 0;
    
    if(!root->rch && !root->lch) return 0;
    
    return 1 + NumBranch(root->rch) + NumBranch(root->lch);
    
}

6-6 二叉树的叶子结点数* (20 分)

int NumLeaf(const NODE *root)
{
    if(root == NULL) return 0;
    
    if(root->rch == NULL && root->lch == NULL) return 1;
    
    return NumLeaf(root->rch) + NumLeaf(root->lch);
}

6-5 二叉树的结点数* (20 分)

int NumNode(const NODE *root)
{
    if(root == NULL) return 0;
    
    return 1 + NumNode(root->lch) + NumNode(root->rch);
}

6-1 求二叉树高度 (20 分)

大无语事件 (x > y ? x : y) + 1;才是对的 ,x > y ? x > y + 1
这是两个东西,注意?符的结束

int GetHeight( BinTree BT )
{
    if(BT == NULL) return 0;

    return (GetHeight(BT->Left) > GetHeight(BT->Right) ? GetHeight(BT->Left) :GetHeight(BT->Right)) + 1;
}

L2-006 树的遍历 (25 分)

不知道为什么还要卡空格和换行,无语++
请添加图片描述
题目链接:

https://pintia.cn/problem-sets/994805046380707840/problems/994805069361299456

#include<bits/stdc++.h>

using namespace std;
const int N = 100;

int q[N],s[N],cn[N],ans[N][N];
int n;
int t = - 1;

void solve(int l , int r , int l1 , int r1 ,int k)
{
	if(l > r) return;
	t = max(t , k);
	int st = q[r];
	ans[k][cn[k] ++] = st;
	int as = 0;
	for(int i = l1 ; i <= r1 ; i ++){
			
		if(st == s[i]){
			solve(l , l + as - 1 , l1 , l1 + as - 1, k + 1) , solve(l + as  , r - 1, l1 + as + 1 , r1 ,k + 1);
			return;
		}
		as ++;
	}
}

int main()
{
	scanf("%d",&n);
	for(int i = 0 ; i < n ; i ++) scanf("%d",&q[i]);
	
	for(int i = 0 ; i < n ; i ++) scanf("%d",&s[i]);
	
	solve(0 , n - 1 , 0 , n - 1 ,0);
	
    int h = 0;
	for(int i = 0 ; i <= t ; i ++)
		for(int j = 0 ; ans[i][j] ; j ++){
            printf("%d", ans[i][j]);
            h ++;
            if(h != n) printf(" ");
            
        }
			
	
    puts("");
	return 0; 
} 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值