首页 > 试题广场 >

小红的二叉树

[编程题]小红的二叉树
  • 热度指数:5601 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1024M,其他语言2048M
  • 算法知识视频讲解
\hspace{15pt}小红想知道,深度为 n满二叉树^{\texttt{[1]}}有多少条长度为 2简单路径^{\texttt{[2]}}?由于答案可能很大,请将答案对 (10^9+7) 取模后输出。
\hspace{15pt}在本题中,两条简单路径所包含的点集不同时,被视为不同的。例如,路径 u-v 与路径 v-u 被视为相同的,因为它们均包含点 u 与点 v

\hspace{15pt}一棵深度为 h满二叉树^{\texttt{[1]}}由恰好 2^h-1 个节点组成,每一个节点要么是叶子节点,要么有 2 个儿子,并且全部叶子节点的深度均为 h
\hspace{15pt}简单路径^{\texttt{[2]}}是指这样一条路径,其经过的顶点和边互不相同。

输入描述:
\hspace{15pt}在一行上输入一个正整数 n \left(1 \leqq n \leqq 10^4\right) 代表满二叉树的深度。


输出描述:
\hspace{15pt}输出一个整数,代表深度为 n 的满二叉树中,长度为 2 的简单路径的数量。由于答案可能很大,请将答案对 (10^9+7) 取模后输出。
示例1

输入

1

输出

0

说明

\hspace{15pt}在这个样例中,深度为 1 的满二叉树只有 1 个节点,所以没有长度为 2 的简单路径。
示例2

输入

3

输出

7

说明

\hspace{15pt}在这个样例中,所给出的满二叉树如下图所示:

头像 CARLJOSEPHLEE
发表于 2025-03-10 22:49:02
n = int(input()) if n == 1: print(0) else: pow1 = pow(2,n-1,1000000007) print((3*pow1-5)%1000000007)
头像 努力努力zzb
发表于 2025-04-04 21:10:28
shendu2 =input() shendu=int(shendu2) MOD=10**9+7 if shendu==1: print(0) else: ZHILI=pow(2,shendu-1,MOD) BABA=3*ZHILI-5 daan=BABA%MOD 展开全文
头像 ading007
发表于 2025-05-23 14:30:17
package main import ( "fmt" ) const mod float64 = 1e9+7 func main() { var h int fmt.Scan(&h) cnt := 0 if h > 1 展开全文
头像 lhp_zml
发表于 2025-03-10 13:46:03
// 从深度为2开始,当前叶子节点为2个。每加一层,每个叶子节点会增加三条路径 #include<bits/stdc++.h> using namespace std; const int mod=1e9+7; int n; int main(){ cin>>n; 展开全文
头像 牛客754921490号
发表于 2025-12-20 21:26:02
#include <iostream> #include <cmath> using namespace std; // 非根且非叶节点数量*3 + 有子节点的根节点 // 2^n n <=1e4 数字巨大,int明显越界,需要提前mod // (ab)%m = (a 展开全文
头像 one_peace_Bo
发表于 2025-08-02 22:02:12
#include <cmath> #include <iostream> using namespace std; int main() { constexpr int MOD = 1e9+7; long long n; while (cin > 展开全文
头像 我没拿98k
发表于 2025-04-03 16:23:50
n=int(input()) if n==1: print(0) else: print((3*2**(n-1)-5)%(10**9+7)) 数学问题(3*pow(2,n-1)) - 5
头像 屁颠屁颠の父
发表于 2025-03-13 15:33:18
depth = int(input()) dp = [0] * depth dp[0] = 0 if depth == 1: print(dp[0]) elif depth == 2: dp[1] = 1 print(dp[1]) else: dp[1] = 1 展开全文
头像 你吃了码
发表于 2025-03-10 16:40:19
import sys deep=int(input().strip()) def f(d): res1=0 res2=0 p1=0 p2=0 if d <=1: return 0 if d==2: return 展开全文
头像 牛客210380430号
发表于 2025-04-14 16:52:46
# import math n=int(input()) if n==1: print(0) else: # nl=int(math.pow(2,n-1)-1) print((3*(2**(n-1)-1)-2)%((10**9)+7))