图的dfs非递归_算法与数据结构基础 - 深度优先搜索(DFS)

96c8a2a49e51d1209284ae911cd028a0.png

DFS基础

深度优先搜索(Depth First Search)是一种搜索思路,相比广度优先搜索(BFS),DFS对每一个分枝路径深入到不能再深入为止,其应用于树/图的遍历、嵌套关系处理、回溯等,可以用递归、堆栈(stack)实现DFS过程。

关于广度优先搜索(BFS)详见:算法与数据结构基础 - 广度优先搜索(BFS)

关于递归(Recursion)详见:算法与数据结构基础 - 递归(Recursion)

树的遍历

DFS常用于二叉树的遍历,关于二叉树详见:

算法与数据结构基础 - 二叉查找树(Binary Search Tree)

算法与数据结构基础 - 二叉树(Binary Tree)

相关LeetCode题:

559. Maximum Depth of N-ary Tree 题解

897. Increasing Order Search Tree 题解

872. Leaf-Similar Trees 题解

108. Convert Sorted Array to Binary Search Tree 题解

100. Same Tree 题解

257. Binary Tree Paths 题解

101. Symmetric Tree 题解

110. Balanced Binary Tree 题解

112. Path Sum 题解

111. Minimum Depth of Binary Tree 题解

979. Distribute Coins in Binary Tree 题解

366. Find Leaves of Binary Tree 题解

1123. Lowest Common Ancestor of Deepest Leaves 题解

1110. Delete Nodes And Return Forest 题解

1026. Maximum Difference Between Node and Ancestor 题解

513. Find Bottom Left Tree Value 题解

515. Find Largest Value in Each Tree Row 题解

199. Binary Tree Right Side View 题解

1145. Binary Tree Coloring Game 题解

337. House Robber III 题解

863. All Nodes Distance K in Binary Tree 题解

1080. Insufficient Nodes in Root to Leaf Paths 题解

114. Flatten Binary Tree to Linked List 题解

129. Sum Root to Leaf Numbers 题解

971. Flip Binary Tree To Match Preorder Traversal 题解

105. Construct Binary Tree from Preorder and Inorder Traversal 题解

113. Path Sum II 题解

109. Convert Sorted List to Binary Search Tree 题解

116. Populating Next Right Pointers in Each Node 题解

430. Flatten a Multilevel Doubly Linked List 题解

124. Binary Tree Maximum Path Sum 题解

99. Recover Binary Search Tree 题解

968. Binary Tree Cameras 题解

图的遍历

树可视作一类特殊的图,更一般地DFS用于图的遍历,可视化过程

关于图详见:算法与数据结构基础 - 图(Graph)

相关LeetCode题:

733. Flood Fill 题解

841. Keys and Rooms 题解

695. Max Area of Island 题解

531. Lonely Pixel I 题解

529. Minesweeper 题解

756. Pyramid Transition Matrix 题解

694. Number of Distinct Islands 题解

711. Number of Distinct Islands II 题解

638. Shopping Offers 题解

851. Loud and Rich 题解

802. Find Eventual Safe States 题解

785. Is Graph Bipartite? 题解

200. Number of Islands 题解

1034. Coloring A Border 题解

886. Possible Bipartition 题解

332. Reconstruct Itinerary 题解

827. Making A Large Island 题解

329. Longest Increasing Path in a Matrix 题解

834. Sum of Distances in Tree 题解

499. The Maze III 题解

嵌套关系处理

DFS可用于形如 "3[a2[c]]" 存在嵌套关系的问题处理,例如 LeetCode题目394. Decode String:

    // 394. Decode String
    string decode(string s,int& pos){
        string res="";
        int num=0;
        for(;pos<s.length();pos++){
            if(s[pos]=='['){
                string tmp=decode(s,++pos);
                for(;num>0;num--) res+=tmp;
            }
            else if(s[pos]>='0'&&s[pos]<='9')
                num=num*10+s[pos]-'0';
                else if(s[pos]==']') return res;
                else
                    res+=s[pos];
        }
        return res;
    }

以上通过DFS进入到最内层的 '[ ]',之后随着函数返回、由内向外层层展开。

相关LeetCode题:

394. Decode String 题解

690. Employee Importance 题解

339. Nested List Weight Sum 题解

364. Nested List Weight Sum II 题解

1020. Number of Enclaves 题解

439. Ternary Expression Parser 题解

DFS与回溯

回溯(Backtracking)算法中选择一条路径并走到底的思路,正是DFS。DFS是构成回溯算法的一部分。

关于回溯详见:算法与数据结构基础 - 回溯(Backtracking)

相关LeetCode题:

494. Target Sum 题解

491. Increasing Subsequences 题解

473. Matchsticks to Square 题解

980. Unique Paths III 题解

679. 24 Game 题解

301. Remove Invalid Parentheses 题解

DFS与Memorization

和BFS过程一样,应用DFS时也有可能重复访问同一节点,这时可用Memorization记录哪些节点已经访问过,避免路径重复遍历。

相关LeetCode题:

851. Loud and Rich 题解

547. Friend Circles 题解

490. The Maze 题解

1059. All Paths from Source Lead to Destination 题解

934. Shortest Bridge 题解

489. Robot Room Cleaner 题解

753. Cracking the Safe 题解

749. Contain Virus 题解

514. Freedom Trail 题解

546. Remove Boxes 题解

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值