題目:輸入一個整數和一棵二元樹。列印出和與輸入整數相等的所有路徑,從樹的根結點開始往下訪問一直到葉結點所經過的所有結點形成一條路徑。
分析:這道題考查對二叉樹遍歷方式的理解,採用後序遍歷,如果把二叉樹看成圖,就是圖的深度遍歷。使用變數存放當前遍歷的路徑和,當訪問到某一結點時,把該結點新增到路徑上,並累加當前結點的值。如果當前結點為葉結點並且當前路徑的和剛好等於輸入的整數,則當前的路徑符合要求,我們把它列印出來。如果當前結點不是葉結點,則繼續訪問它的子結點。當前結點訪問結束後,遞迴函式將自動回到父結點,因此我們在函式退出之前要在路徑上刪除當前結點並減去當前結點的值,以確保返回父結點時路徑剛好是根結點到父結點的路徑。
二元樹結點的資料結構定義為:
- struct BSTreeNode
- {
- struct BSTreeNode()
- {
- m_pLeft = NULL;
- m_pRight = NULL;
- }
- int m_nValue; // value of node
- BSTreeNode *m_pLeft; // left child of node
- BSTreeNode *m_pRight; // right child of node
- };
遞迴求解程式碼如下:
- ///////////////////////////////////////////////////////////////////////
- // Find paths whose sum equal to expected sum
- ///////////////////////////////////////////////////////////////////////
- void FindPath
- (
- BSTreeNode* pTreeNode, // a node of binary tree
- int expectedSum, // the expected sum
- std::vector<int>& path, // a path from root to current node
- int& 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 path
- bool isLeaf = (!pTreeNode->m_pLeft && !pTreeNode->m_pRight);
- if(currentSum == expectedSum && isLeaf)
- {
- std::vector<int>::iterator iter = path.begin();
- for(; iter != path.end(); ++ iter)
- printf("%d\t",*iter);
- printf("\n");
- }
- // if the node is not a leaf, goto its children
- if(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 sum
- currentSum -= pTreeNode->m_nValue;//我認為沒有必要傳currentSum的引用,所以在這裡也不需要減
- path.pop_back();
- }
- </int></int>
擴充套件題:如果將條件放寬,計算和的路徑改為從根節點到葉子節點路徑上任意連續子路徑呢?
條件改變之後,難度有所增加,堆疊中光存放從root到當前節點的和顯然不夠,需要對堆疊中的元素做出改變,使之存放堆疊當前位置到當前遍歷節點的路徑和,元素型別定義如下:
- class StackElement
- {
- public:
- StackElement(struct BSTreeNode* node, int s){pNode = node;sum = s;};
- void AddValue(int v){sum+=v;}
- int GetValue(){return sum;}
- struct BSTreeNode* pNode; //二叉樹節點指標,由於不採用遞迴,因此必須保持節點指標
- int sum; //從當前遍歷節點到*pNode這段路徑的和
- };
在這裡不適用遞迴,而採用另外的一種求解方式。
- void BSTreeSumWay(struct BSTreeNode* root,int sum)
- {
- assert(root);
- vector<stackelement> way;
- while (root || !way.empty())
- {
- while(root)
- {
- StackElement temp(root,0);
- way.push_back(temp);
- //路徑上增加了root,因此從way_iter->pNode到*root的路徑sum要更新
- vector<stackelement>::iterator way_iter = way.begin();
- for (;way_iter != way.end(); way_iter++)
- {
- way_iter->AddValue(root->m_nValue);
- if (sum == way_iter->GetValue())
- {
- //列印路徑
- vector<stackelement>::iterator print_iter = way_iter;
- for (;print_iter != way.end();print_iter++)
- {
- printf("%d\t",print_iter->pNode->m_nValue);
- }
- printf("\n");
- }
- }
- root = root->m_pLeft;
- }
- //右子樹為空或者剛從右子樹返回
- while (way.rbegin()->pNode->m_pRight==NULL || root==way.rbegin()->pNode->m_pRight)
- {
- root = way.rbegin()->pNode;
- //路徑上減少了最後一個節點,因此路徑sum要更新
- int v = -way.rbegin()->pNode->m_nValue;
- way.pop_back();
- vector<stackelement>::iterator way_iter = way.begin();
- for (;way_iter != way.end(); way_iter++)
- {
- way_iter->AddValue(v);
- }
- if (way.empty())
- {
- break;
- }
- }
- if (way.empty())
- break;
- root = way.rbegin()->pNode->m_pRight;
- }
- }
- </stackelement></stackelement></stackelement></stackelement>
網路轉載請註明:轉載自程式設計師面試之家