深度优先非递归实现算法: 1 递归算法: //初始化相关数据结构 DFS(G) ----------------------------------------------------------------------------------- 1 for each vertex u ∈ G.V 2 u.color ← WHITE // paint all vertices white; undiscovered 3 u.π ← NIL 4 time ← 0 // global variabl…
Depth first search is a graph search algorithm that starts at one node and uses recursion to travel as deeply down a path of neighboring nodes as possible, before coming back up and trying other paths. const {createQueue} = require('./queue'); func…
最近做的题记录下. 258. Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. For example: Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. int addDigi…
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 求二叉树的最大深度问题用到深度优先搜索DFS,递归的完美应用,跟求二叉树的最小深度问题原理相同.代码如下: C++ 解法一: class Solution { public: in…
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7…