日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

careercup-树与图 4.9

發布時間:2025/5/22 编程问答 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 careercup-树与图 4.9 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

4.9 給定一顆二叉樹,其中每個結點都含有一個數值。設計一個算法,打印結點數值總和等于某個給定值的所有路徑。注意,路徑不一定非得從二叉樹的根節點或葉子節點開始或結束。

?

類似于leetcode:Path Sum II

C++實現代碼:(使用了雙重的遞歸)對于不含有parent指針域時。

#include<iostream> #include<new> #include<vector> using namespace std;//Definition for binary tree struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {} };class Solution { public:vector<vector<int> > path;vector<vector<int> > pathSum(TreeNode *root, int sum){vector<int> tmp;hasPathSum(root,sum,tmp);//改變開始的節點,不一定要從根結點開始,遍歷從每一個節點開始if(root->left)pathSum(root->left,sum);if(root->right)pathSum(root->right,sum);return path;}void hasPathSum(TreeNode *root, int sum,vector<int> tmp){if(root==NULL)return;tmp.push_back(root->val);//改變結束的地方,不一定要到葉子節點if((sum-root->val)==0){path.push_back(tmp);}if(root->left)hasPathSum(root->left,sum-root->val,tmp);if(root->right)hasPathSum(root->right,sum-root->val,tmp);}void createTree(TreeNode *&root){int i;cin>>i;if(i!=0){root=new TreeNode(i);if(root==NULL)return;createTree(root->left);createTree(root->right);}} }; int main() {Solution s;TreeNode *root;s.createTree(root);vector<vector<int> > path=s.pathSum(root,6);for(auto a:path){for(auto v:a)cout<<v<<" ";cout<<endl;} }

?方法二:如果結點中包含指向父親結點的指針,那么,只需要去遍歷這棵二叉樹, 然后從每個結點開始,不斷地去累加上它父親結點的值直到父親結點為空(這個具有唯一性, 因為每個結點都只有一個父親結點。也正因為這個唯一性, 可以不另外開額外的空間來保存路徑),如果等于給定的值sum,則打印輸出。

實現的方法:

void find_sum(Node* head, int sum){if(head == NULL) return;Node *no = head;int tmp = 0;for(int i=1; no!=NULL; ++i){tmp += no->key;if(tmp == sum)print(head, i);no = no->parent;}find_sum(head->lchild, sum);find_sum(head->rchild, sum); }

打印輸出時,只需要提供當前結點的指針,及累加的層數即可。然后從當前結點開始, 不斷保存其父親結點的值(包含當前結點)直到達到累加層數,然后逆序輸出即可。

代碼如下:

void print(Node* head, int level){vector<int> v;for(int i=0; i<level; ++i){v.push_back(head->key);head = head->parent;}while(!v.empty()){cout<<v.back()<<" ";v.pop_back();}cout<<endl; }

方法三:如果結點中不包含指向父親結點的指針,則在二叉樹從上向下查找路徑的過程中, 需要為每一次的路徑保存中間結果,累加求和仍然是從下至上的,對應到保存路徑的數組, 即是從數組的后面開始累加的,這樣能保證遍歷到每一條路徑。

代碼如下:

void print2(vector<int> v, int level){for(int i=level; i<v.size(); ++i)cout<<v.at(i)<<" ";cout<<endl; } void find_sum2(Node* head, int sum, vector<int> v, int level){if(head == NULL) return;v.push_back(head->key);int tmp = 0;for(int i=level; i>-1; --i){tmp += v.at(i);if(tmp == sum)print2(v, i);}vector<int> v1(v), v2(v);find_sum2(head->lchild, sum, v1, level+1);find_sum2(head->rchild, sum, v2, level+1); }

方法二 完整代碼:

#include<iostream> #include<new> #include<map> #include<vector> using namespace std;struct BinarySearchTree {int elem;BinarySearchTree *parent;BinarySearchTree *left;BinarySearchTree *right;BinarySearchTree(int x):elem(x),parent(NULL),left(NULL),right(NULL) {} };void insert(BinarySearchTree *&root,int z) {BinarySearchTree *y=new BinarySearchTree(z);if(root==NULL){root=y;return;}else if(root->left==NULL&&z<root->elem){root->left=y;y->parent=root;return;}else if(root->right==NULL&&z>root->elem){root->right=y;y->parent=root;return;}if(z<root->elem)insert(root->left,z);elseinsert(root->right,z); }void createBST(BinarySearchTree *&root) {int arr[10]= {10,4,6,1,8,3,0,7,2,9};for(auto a:arr)insert(root,a); }//使用level的原因就是因為,不一定要到根,只有根的父節點為NULL void print(BinarySearchTree *head,int level) {vector<int> vec;for(int i=0;i<level;++i){vec.push_back(head->elem);head=head->parent;}while(!vec.empty()){cout<<vec.back()<<" ";vec.pop_back();}cout<<endl; } //root選擇的是當前結束的節點,也就是從下往上開始最下面的節點,而node是往上找到的剛好滿足的最后一個結點,root是在不斷加深的 void find_sum(BinarySearchTree *root,int sum) {if(root==NULL)return;BinarySearchTree *node=root;int tmp=0;for(int i=1;node!=NULL;++i){tmp+=node->elem;if(tmp==sum)print(root,i);node=node->parent;}find_sum(root->left,sum);find_sum(root->right,sum); } int main() {BinarySearchTree *root=NULL;createBST(root);cout<<"find sum is: "<<endl;find_sum(root,10);return 0; } View Code

方法三 完整代碼:

#include<iostream> #include<new> #include<map> #include<vector> using namespace std;struct BinarySearchTree {int elem;BinarySearchTree *parent;BinarySearchTree *left;BinarySearchTree *right;BinarySearchTree(int x):elem(x),parent(NULL),left(NULL),right(NULL) {} };void insert(BinarySearchTree *&root,int z) {BinarySearchTree *y=new BinarySearchTree(z);if(root==NULL){root=y;return;}else if(root->left==NULL&&z<root->elem){root->left=y;y->parent=root;return;}else if(root->right==NULL&&z>root->elem){root->right=y;y->parent=root;return;}if(z<root->elem)insert(root->left,z);elseinsert(root->right,z); }void createBST(BinarySearchTree *&root) {int arr[10]= {10,4,6,1,8,3,0,7,2,9};for(auto a:arr)insert(root,a); }//使用level記錄選擇v中的從哪個下標開始相加 void print(vector<int> v,int level) {for(int i=level;i<v.size();++i)cout<<v[i]<<" ";cout<<endl; } //root開始,將當前層的值加入v中 void find_sum(BinarySearchTree *root,int sum,vector<int> v,int level) {if(root==NULL)return;v.push_back(root->elem);int tmp=0;for(int i=level;i>-1;--i){tmp+=v[i];if(tmp==sum)print(v,i);}//每一層將當前層的結點的值放入v中,由于不是傳遞的引用,所以同一層放入v中的值不會影響,從root結點開始保存每一層的find_sum(root->left,sum,v,level+1);find_sum(root->right,sum,v,level+1); } int main() {BinarySearchTree *root=NULL;createBST(root);vector<int> v;cout<<"find sum is: "<<endl;find_sum(root,10,v,0);return 0; } View Code

總結

以上是生活随笔為你收集整理的careercup-树与图 4.9的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。