算法-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
原创粉丝点击