【二叉樹上的路徑數】及【過樹上兩個節點最長路徑】
#include<bits/stdc++.h> using namespace std; const int maxn=1e3+20; vector<int>G[maxn]; int x,y,ans,n; int dfs(int v){ int res=0; //for(auto &i:G[v]) i 遍歷後面容器中的每個元素 for(int j=0;j<G[v].size();j++){ int &i=G[v][j]; res+=dfs(i); } ans+=res; return res+1; } int main(){ while(scanf("%d",&n)!=EOF){ for(int i=1;i<n;i++){ scanf("%d%d",&x,&y); G[x].push_back(y); } ans=0; dfs(1); cout<<ans<<endl; } return 0; }
相關推薦
【二叉樹上的路徑數】及【過樹上兩個節點最長路徑】
#include<bits/stdc++.h> using namespace std; const int maxn=1e3+20; vector<int>G[maxn]; int x,y,ans,n; int dfs(int v){ int
求二叉樹中兩個節點最遠的距離
一說到二叉樹,就有很多題目,今天在程式設計之美中看到了二叉樹中兩個節點最遠的距離。所以給想借機寫一篇部落格。 在開始之前,我們先想想,兩個最常節點的最遠距離是怎麼樣的? 情況一:最大距離可能一個在左子
LeetCode.687 Longest Univalue Path (經典的求相同節點最長路徑)
題目: Given a binary tree, find the length of the longest path where each node in the path has the
【二叉樹】SDUT 3342 資料結構實驗之二叉樹三:統計葉子數
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,,de,g,,f,,, (其中,表示空結點)。請建立二叉樹並求二叉樹的葉子結點個數。 Input 連續輸入多組資料,每組資料輸入一個長度小於50個字元的字串。 Output 輸出
【二叉樹】指定結點到根節點的路徑
利用後序遍歷非遞迴的思想,結合棧。 如果某結點的左右子樹均未找到指定結點,則將該結點出棧,那就需要標記一下當前結點的訪問狀態。 直到找到結點,或者遍歷完樹仍未找到(此時棧空)。 typedef struct BNode { int data; BNode
【資料結構】二叉樹的遍歷及應用
前言 在二叉樹的應用中,常常要求在樹中查詢某些結點,或者對樹中的結點統一進行某種處理。因此,就提到了二叉樹的遍歷問題,對於線性結構來說,遍歷是一個很容易解決的問題,而二叉樹偏偏是一種非線性的結構,因此需要尋找一種規律。 二叉樹由三個基本單
【二叉樹】最大路徑和【124. Binary Tree Maximum Path Sum】
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *le
劍指offer第二十四題【二叉樹中和為某一值的路徑】c++實現
題目描述 輸入一顆二叉樹和一個整數,打印出二叉樹中結點值的和為輸入整數的所有路徑。路徑定義為從樹的根結點開始往下一直到葉結點所經過的結點形成一條路徑。 先序遍歷二叉樹,用一個數組記錄當前路徑,如果遍歷到葉子節點就進行判斷是否和給定值相等。 class Solution { public: vect
SDNU 1015.最遠路徑【二叉樹DFS】
1015.最遠路徑 Description 有一棵有n個節點的二叉樹,它的節點編號為1到n,根節點編號是1,它的每條邊都有一個給定的長度。請你求出該二叉樹中距離根節點最遠的節點。 I
POJ1195 Mobile phones 【二維樹狀數組】
chan mono ins written writing ram cor data- amp Mobile phones Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 1428
【二維樹狀數組】See you~
get iostream closed href typedef 線段樹 比較 space 線段 https://www.bnuoj.com/v3/contest_show.php?cid=9148#problem/F 【題意】 給定一個矩陣,每個格子的初始值為1。現在可以
1064. Complete Binary Search Tree (30)【二叉樹】——PAT (Advanced Level) Practise
function namespace his () 技術 androi sed tel evel 題目信息 1064. Complete Binary Search Tree (30) 時間限制100 ms 內存限制65536 kB 代碼長度限制
HDU 5517 【二維樹狀數組///三維偏序問題】
void blog scan memset 集合 while ++ struct name 題目鏈接:【http://acm.split.hdu.edu.cn/showproblem.php?pid=5517】 題意:定義multi_set A<a , d>,B
【二叉樹】二叉樹遍歷總結
struct left else oot nor 節點 操作 preorder AC 節點定義如下 1 // Definition for a binary tree node. 2 struct TreeNode { 3 int val; 4 Tre
UVa 679 - Dropping Balls【二叉樹】【思維題】
get title lld -s round () color 直接 2個 題目鏈接 題目大意: 小球從一棵所有葉子深度相同的二叉樹的頂點開始向下落,樹開始所有節點都為0。若小球落到節點為0的則往左落,否則向右落。並且小球會改變它經過的節點,0變1,1變0。給定樹的深度
如何求先序排列和後序排列——hihocoder+洛谷例題【二叉樹遞歸搜索】
define second [] tor 記錄 例題 .com 內存限制 行為 【已知先序、中序求後序排列】: [#1049 : 後序遍歷](http://hihocoder.com/problemset/problem/1049) 時間限制:10000ms 單點時限:1
【二維樹狀數組】【CF10D】 LCIS
chan wap 傳送門 樹狀 lcis tdi swa long long code 傳送門 Description 給你兩個串,求他們的最長公共上升子序列 Input 第一行是第一個串的長度\(n\) 第二行\(n\)個數代表第一個串 第三行是第二個串的長度\(m\)
#117-【二叉樹】FBI樹
Description 我們可以把由“0”和“1”組成的字串分為三類:全“0”串稱為B串,全“1”串稱為I串,既含“0”又含“1”的串則稱為F串。 FBI樹是一種二叉樹1,它的結點型別也包括F結點,B結點和I結點三種。由一個長度為2N的“01”串S可以構造出一棵FBI樹T,遞迴的構造方法
二叉樹的結構體表示【摘抄自嚴長生老師的網站】
採用鏈式儲存 typedef struct BiTNode{ TElemType data;//資料域 struct BiTNode *lchild,*rchild;//左右孩子指標 }BiTNode,*BiTree; 若需訪問父節點,可如下表示 typedef str
【二叉樹】SDUT 3341 遍歷二叉樹
Problem Description 已知二叉樹的一個按先序遍歷輸入的字元序列,如abc,,de,g,,f,,, (其中,表示空結點)。請建立二叉樹並按中序和後序的方式遍歷該二叉樹。 Input 連續輸入多組資料,每組資料輸入一個長度小於50個字元的字串。 Output