uva 297(二叉树)

该博客介绍了如何解决UVA 297问题,通过建立两棵二叉树并进行深度优先搜索(DFS)。在搜索过程中,注意到随着树的层级加深,每个节点值会按特定规律递减,因此在计算累加值时应用了1024 / pow(4, l)的公式,其中l表示当前节点的深度。" 110676846,9090805,数据库规范化原理与函数依赖,"['数据库理论', '数据模型', '函数依赖', '范式理论']
摘要由CSDN通过智能技术生成

题解:建两棵树然后深搜遍历,需要注意在深搜的函数里随着树的层次变深,每个黑色小方块的值也在不断减小,所以累加的值是 1024 / pow (4, l),l是数的深度。

#include <cstdio>
#include <string>
#include <iostream>
#include <cmath>
using namespace std;
const int N = 10010;

struct Node {
    char c;
    Node* s[4];
    Node() {
        c = '\0';
        s[0] = s[1] = s[2] = s[3] = NULL;
    }
};
int len, sum, i;

Node* build(string str) {
    i++;
    if (i == len)
        return NULL;
    Node* node = new Node;
    node -> c = str[i];
    if (node -> c == 'p') {
        node -> s[0] = build(str);
        node -> s[1] = build(str);
        node -> s[2] = build(str);
        node -> s[3] = build(str);
    }
    return node;
}

void dfs(Node* node1, Node* node2, int l) {
    if (node1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值