在這個問題中,我們得到一棵二元樹,我們需要從特定節點執行 dfs,其中我們假設給定節點作為根並從中執行 dfs。
在上面的樹中假設我們需要執行DFS節點F
在本教程中,我們將應用一些非正統的方法,以便大大降低我們的時間複雜度,因此我們也能夠在更高的約束條件下運行此程式碼。
方法 - 在這種方法中,我們不會簡單地採用天真的方法,即我們簡單地對每個節點應用dfs,因為它不適用於更高的約束,因此我們嘗試使用一些非正統的方法來避免獲得TLE。
#include <bits/stdc++.h> using namespace std; #define N 100000 // Adjacency list to store the // tree nodes connections vector<int> v[N]; unordered_map<int, int> mape; // will be used for associating the node with it's index vector<int> a; void dfs(int nodesunder[], int child, int parent){ // function for dfs and precalculation our nodesunder a.push_back(child); // storing the dfs of our tree // nodesunder of child subtree nodesunder[child] = 1; for (auto it : v[child]) { // performing normal dfs if (it != parent) { // as we the child can climb up to //it's parent so we are trying to avoid that as it will become a cycle dfs(nodesunder, it, child); // recursive call nodesunder[child] += nodesunder[it]; // storing incrementing the nodesunder //by the number of nodes under it's children } } } // Function to print the DFS of subtree of node void printDFS(int node, int nodesunder[]){ int ind = mape[node]; // index of our node in the dfs array cout << "The DFS of subtree " << node << ": "; // print the DFS of subtree for (int i = ind; i < ind + nodesunder[node]; i++){ // going through dfs array and then //printing all the nodes under our given node cout << a[i] << " "; } cout << endl; } void addEdgetoGraph(int x, int y){ // for maintaining adjacency list v[x].push_back(y); v[y].push_back(x); } void mark(){ // marking each node with it's index in dfs array int size = a.size(); // marks the index for (int i = 0; i < size; i++) { mape[a[i]] = i; } } int main(){ int n = 7; // add edges of a tree addEdgetoGraph(1, 2); addEdgetoGraph(1, 3); addEdgetoGraph(2, 4); addEdgetoGraph(2, 5); addEdgetoGraph(4, 6); addEdgetoGraph(4, 7); // array to store the nodes present under of subtree // of every node in a tree int nodesunder[n + 1]; dfs(nodesunder, 1, 0); // generating our nodesunder array mark(); // marking the indices in map // Query 1 printDFS(2, nodesunder); // Query 2 printDFS(4, nodesunder); return 0; }
輸出
The DFS of subtree 2: 2 4 6 7 5 The DFS of subtree 4: 4 6 7
理解程式碼
在這個方法中,我們預先計算dfs 的順序並將其儲存在向量中,當我們預先計算dfs 時,我們也計算從每個節點開始的每個子樹下存在的節點,然後我們只需從then 節點的起始索引遍歷到其子樹中存在的所有節點數。
結論
在本教程中,我們解決了一個問題來解決以下查詢:樹中子樹的 DFS。我們也學習了針對此問題的C 程序以及解決此問題的完整方法(Normal)。
我們可以用其他語言(例如C、java、python等語言)寫相同的程式。希望這篇文章對您有幫助。
以上是在一棵樹中,使用C++查詢子樹的深度優先搜索的詳細內容。更多資訊請關注PHP中文網其他相關文章!

C#和C 在性能上的差異主要體現在執行速度和資源管理上:1)C 在數值計算和字符串操作上通常表現更好,因為它更接近硬件,沒有垃圾回收等額外開銷;2)C#在多線程編程上更為簡潔,但性能略遜於C ;3)選擇哪種語言應根據項目需求和團隊技術棧決定。

1)c relevantduetoItsAverity and效率和效果臨界。 2)theLanguageIsconTinuellyUped,withc 20introducingFeaturesFeaturesLikeTuresLikeSlikeModeLeslikeMeSandIntIneStoImproutiMimproutimprouteverusabilityandperformance.3)

C 在現代世界中的應用廣泛且重要。 1)在遊戲開發中,C 因其高性能和多態性被廣泛使用,如UnrealEngine和Unity。 2)在金融交易系統中,C 的低延遲和高吞吐量使其成為首選,適用於高頻交易和實時數據分析。

C 中有四種常用的XML庫:TinyXML-2、PugiXML、Xerces-C 和RapidXML。 1.TinyXML-2適合資源有限的環境,輕量但功能有限。 2.PugiXML快速且支持XPath查詢,適用於復雜XML結構。 3.Xerces-C 功能強大,支持DOM和SAX解析,適用於復雜處理。 4.RapidXML專注於性能,解析速度極快,但不支持XPath查詢。

C 通過第三方庫(如TinyXML、Pugixml、Xerces-C )與XML交互。 1)使用庫解析XML文件,將其轉換為C 可處理的數據結構。 2)生成XML時,將C 數據結構轉換為XML格式。 3)在實際應用中,XML常用於配置文件和數據交換,提升開發效率。

C#和C 的主要區別在於語法、性能和應用場景。 1)C#語法更簡潔,支持垃圾回收,適用於.NET框架開發。 2)C 性能更高,需手動管理內存,常用於系統編程和遊戲開發。

C#和C 的歷史與演變各有特色,未來前景也不同。 1.C 由BjarneStroustrup在1983年發明,旨在將面向對象編程引入C語言,其演變歷程包括多次標準化,如C 11引入auto關鍵字和lambda表達式,C 20引入概念和協程,未來將專注於性能和系統級編程。 2.C#由微軟在2000年發布,結合C 和Java的優點,其演變注重簡潔性和生產力,如C#2.0引入泛型,C#5.0引入異步編程,未來將專注於開發者的生產力和雲計算。

C#和C 的学习曲线和开发者体验有显著差异。1)C#的学习曲线较平缓,适合快速开发和企业级应用。2)C 的学习曲线较陡峭,适用于高性能和低级控制的场景。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

ZendStudio 13.5.1 Mac
強大的PHP整合開發環境