算法-DFS和BFS
来源:互联网 发布:linux mint如何换字体 编辑:程序博客网 时间:2024/05/08 03:42
DFS
- 与二叉树相结合
- 与图相结合:时间复杂度为O(V+E)
Recover BST
void recoverTree(TreeNode* root){ TreeNode* first=NULL; TreeNode* second=NULL; TreeNode* pre=NULL; TreeNode* node=root; if(!node) return; stack<TreeNode*> s; while(node || !s.empty(){ if(node!=NULL){ s.push(node); node=node->left; }else{ node=s.top(); s.pop(); if(pre!=NULL){ if(node->val<pre->val){ if(first==NULL) first=pre; if(first!=NULL) second=node; } } pre=node; node=node->right; } } int tmp=first->val; first->val=second->val; second->val=tmp;}
Path SumI
bool hasPathSum(TreeNode* root, int sum){ if(!root) return false; if(!root->left && !root->right) return root->val==sum; return hasPathSum(root->left,sum-root->val) ||hasPathSum(root->right,sum-root->val);}
Path SumII
vector<vector<int>> pathSum(TreeNode* root, int sum) { vector<vector<int> > paths; vector<int> path; FindPath(root, sum, path, paths); return paths;}void FindPath(TreeNode* root, int sum, vector<int> &path, vector<vector<int> > &paths){ if(!root) return; path.push_back(root->val); if(!root->left && !root->right && sum==root->val) paths.push_back(path); FindPath(root->left,sum-root->Val,path,paths); FindPath(root->right,sum-root->val,path,paths); path.pop_back();}
Populating Next Right Pointers in Each NodeI
void connect(TreeLinkNode *root) { if(!root) return; while(root->left){ TreeLinkNode* p=root; while(p){ p->left->next=p->right; if(p->next) p->right->next=p->next->left; p=p->next; } root=root->left; }}
Populating Next Right Pointers in Each NodeII
void connect(TreeLinkNode *root) { if(!root) return; while(root){ TreeLinkNode* tmp=new TreeLinkNode(0); TreeLinkNode* cur=tmp; while(root){ if(root->left){ cur->next=root->left; cur=cur->next; } if(root->right){ cur->next=root->right; cur=cur->next; } root=root->next; } root=tmp->next; }}
Binary Tree Maximum Path Sum
int result;//global variableint maxPathSum(TreeNode* root){ result=INT_MIN; pathDown(root); return result;}int pathDown(TreeNode* root){ if(!root) return 0; int left=max(0,pathDown(root->left)); int right=max(0,pathDown(root->right)); result=max(result,left+right+root->val); return max(left,right)+root->val;}
Sum Root to Leaf Numbers
int sum;int sumtoLeaf(TreeNode *root){ sum=0; if(root==NULL) return 0; dfs(root,0); return sum;}void dfs(TreeNode *root, int curSum){ curSum=10*curSum+root->val; if(!root->left && !root->right) sum+=curSum; if(root->left) dfs(root->left,curSum); if(root->right)dfs(root->right,curSum);}
Binary Tree paths
vector<string> BTPath(TreeNode* root){ vector<string> res; if(!root) return res; BTPaths(root,res,to_string(root->val));}void BTPaths(TreeNode* root, vector<string>&res, string str){ if(!root->left && !root->right){ res.push(str); return; } if(root->left) BTPaths(root->left,res,str+"->"+to_string(root->left->val)); if(root->right)BTPaths(root->right,res,str+"->"+to_string(root->right->val));}
BFS:广度优先搜索
- Prime的最小生成树算法
- Dijkstra的单元最短路径算法
0 0
- BFS和DFS算法
- BFS和DFS算法
- 算法-DFS和BFS
- DFS和BFS算法介绍
- BFS和DFS优先搜索算法
- BFS和DFS优先搜索算法
- BFS和DFS优先搜索算法
- BFS和DFS优先搜索算法
- 【算法模板之DFS和BFS】
- BFS和DFS优先搜索算法
- 图的DFS和BFS算法
- BFS和DFS优先搜索算法
- 图的遍历:DFS和BFS算法
- 图的遍历算法(DFS和BFS)
- 《算法导论》BFS和DFS实现
- 【图算法】(2)DFS和BFS
- BFS DFS算法,和动态规划
- 图的BFS和DFS算法
- 部分Android手机拍照后照片被旋转的解决方案
- 利用McAfee SiteList.xml 获取域控制账号密码
- NSData和NSMutableData存储的是二进制数据
- UVaOJ 12304 2D Geometry 110 in 1!
- 小程序的一种轻量级记录日志的方法
- 算法-DFS和BFS
- java线程面试top50
- FS
- RxJava学习笔记(1) - RxJava简介
- Python图表绘制:matplotlib绘图库入门
- .net中使用cookie
- Android下数据库线程安全问题
- POJ 1505(二分+贪心)
- RouterhunterBR 2.0 - 路由器漏洞自动化扫描工具