二元树中和为某一值的所有路径
?
題目:輸入一個整數(shù)和一棵二元樹。從樹的根結點開始往下訪問一直到葉結點所經過的所有結點形成一條路徑。打印出和與輸入整數(shù)相等的所有路徑。
例如輸入整數(shù)22和如下二元樹
??????????????????????????????????????????? 10
?????????????????????????????????????????? /?? \
????????????????????????????????????????? 5???? 12
? ????????????????????????????????????? / ? \???
???????? ??????????????????????????? 4???? 7?
則打印出兩條路徑:10, 12和10, 5, 7。
二元樹結點的數(shù)據(jù)結構定義為:
struct BinaryTreeNode // a node in the binary tree {int m_nValue; // value of nodeBinaryTreeNode *m_pLeft; // left child of nodeBinaryTreeNode *m_pRight; // right child of node };?
分析:這是百度的一道筆試題,考查對樹這種基本數(shù)據(jù)結構以及遞歸函數(shù)的理解。
當 訪問到某一結點時,把該結點添加到路徑上,并累加當前結點的值。如果當前結點為葉結點并且當前路徑的和剛好等于輸入的整數(shù),則當前的路徑符合要求,我們把 它打印出來。如果當前結點不是葉結點,則繼續(xù)訪問它的子結點。當前結點訪問結束后,遞歸函數(shù)將自動回到父結點。因此我們在函數(shù)退出之前要在路徑上刪除當前 結點并減去當前結點的值,以確保返回父結點時路徑剛好是根結點到父結點的路徑。我們不難看出保存路徑的數(shù)據(jù)結構實際上是一個棧結構,因為路徑要與遞歸調用 狀態(tài)一致,而遞歸調用本質就是一個壓棧和出棧的過程。
參考代碼:
?
/// // Find paths whose sum equal to expected sum /// void FindPath (BinaryTreeNode* pTreeNode, // a node of binary treeint expectedSum, // the expected sumstd::vector<int>& path, // a path from root to current nodeint& currentSum // the sum of path ) {if(!pTreeNode)return;currentSum += pTreeNode->m_nValue;path.push_back(pTreeNode->m_nValue);// if the node is a leaf, and the sum is same as pre-defined, // the path is what we want. print the pathbool isLeaf = (!pTreeNode->m_pLeft && !pTreeNode->m_pRight);if(currentSum == expectedSum && isLeaf){ std::vector<int>::iterator iter = path.begin();for(; iter != path.end(); ++ iter)std::cout << *iter << '\t';std::cout << std::endl;}// if the node is not a leaf, goto its childrenif(pTreeNode->m_pLeft)FindPath(pTreeNode->m_pLeft, expectedSum, path, currentSum);if(pTreeNode->m_pRight)FindPath(pTreeNode->m_pRight, expectedSum, path, currentSum);// when we finish visiting a node and return to its parent node,// we should delete this node from the path and // minus the node's value from the current sumcurrentSum -= pTreeNode->m_nValue;path.pop_back(); }?
?
來源:http://zhedahht.blog.163.com/blog/static/254111742007228357325/
?
轉載于:https://www.cnblogs.com/heyonggang/p/3400013.html
總結
以上是生活随笔為你收集整理的二元树中和为某一值的所有路径的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: IDEA---SpringBoot同一个
- 下一篇: 实现一个用户取过的数据不被其他用户取到