<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Breadth First Search &#8211; JSAlgorithm</title>
	<atom:link href="https://jsalgorithm.com/category/breadth-first-search/feed/" rel="self" type="application/rss+xml" />
	<link>https://jsalgorithm.com</link>
	<description></description>
	<lastBuildDate>Thu, 14 May 2020 03:08:02 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.5.15</generator>
<site xmlns="com-wordpress:feed-additions:1">177240629</site>	<item>
		<title>Search Suggestion System (Trie)</title>
		<link>https://jsalgorithm.com/2020/05/14/search-suggestion-system-trie/</link>
					<comments>https://jsalgorithm.com/2020/05/14/search-suggestion-system-trie/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Thu, 14 May 2020 03:08:02 +0000</pubDate>
				<category><![CDATA[Breadth First Search]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[Trie]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[breadth-first-search]]></category>
		<category><![CDATA[leetcode]]></category>
		<category><![CDATA[trie]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=68</guid>

					<description><![CDATA[let suggestedProducts = function(products, searchWord) { const MAX_RESULT_COUNT = 3; let root = { children: {} }; function addToNode(word, index, node) { if (index &#62;= word.length) { node.end = true; return; } // const child = node.children[word[index]]; if (child) { addToNode(word, index + 1, child); } else { let child = {val: word[index], parent: node, [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">let suggestedProducts = function(products, searchWord) {<br>    const MAX_RESULT_COUNT = 3;<br>    let root = {<br>        children: {}<br>    };<br><br>    function addToNode(word, index, node) {<br>        if (index &gt;= word.length) {<br>            node.end = true;<br>            return;<br>        }<br>        //<br>        const child = node.children[word[index]];<br>        if (child) {<br>            addToNode(word, index + 1, child);<br>        }<br>        else {<br>            let child = {val: word[index], parent: node, children: {}};<br>            node.children[word[index]] = child;<br>            addToNode(word, index + 1, child);<br>        }<br>    }<br><br>    function getWord(leaf) {<br>        let node = leaf;<br>        let str = '';<br>        while (node) {<br>            if (node.val) str = node.val + str; //?<br>            node = node.parent; //?<br>        }<br><br>        return str; //?<br>    }<br><br>    function searchProducts(searchString) {<br><br>        let words = [];<br>        let nodes = [...<strong><em>Object</em></strong>.entries(root.children)], len = searchString.length;<br>        while (nodes.length) {<br><br>            let currentNodeLength = nodes.length;<br>            while (currentNodeLength &gt; 0) {<br>                currentNodeLength--;<br><br>                const node = nodes.pop();<br>                const [key, val] = node;<br>                if (len &gt; 0 &amp;&amp; key === searchString[searchString.length - len]) {<br>                    nodes.unshift(...<strong><em>Object</em></strong>.entries(val.children));<br><br>                    if (len === 1 &amp;&amp; val.end) {<br>                        words.push(getWord(val));<br>                    }<br>                } else if (len &lt;= 0) {<br>                    if (val.end) words.push(getWord(val));<br>                    nodes.unshift(...<strong><em>Object</em></strong>.entries(val.children));<br>                }<br>            }<br>            len--;<br>        }<br><br>        return words.sort().slice(0, <strong><em>Math</em></strong>.min(MAX_RESULT_COUNT, words.length));<br>    }<br><br>    for(let product of products) {<br>        addToNode(product, 0, root);<br>    }<br><br><br>    let result = [];<br>    for (let i = 0; i &lt; searchWord.length; i++) {<br>        result.push(searchProducts(searchWord.substring(0, i + 1))); //?<br>    }<br><br>    return result; //?<br>};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/14/search-suggestion-system-trie/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">68</post-id>	</item>
		<item>
		<title>Oranges Rotting</title>
		<link>https://jsalgorithm.com/2020/05/13/oranges-rotting/</link>
					<comments>https://jsalgorithm.com/2020/05/13/oranges-rotting/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Wed, 13 May 2020 01:51:05 +0000</pubDate>
				<category><![CDATA[Breadth First Search]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[breadth-first-search]]></category>
		<category><![CDATA[leetcode]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=56</guid>

					<description><![CDATA[let orangesRotting = function(grid) { const FRESH = 1, ROTTEN = 2, VISITED = -1; let rotten = [], totalFresh = 0, totalEmpty = 0, total = 0; function getAdjacents(row, col) { let result = []; // check top if (row > 0 &#38;&#38; grid[row -1][col] === FRESH) { grid[row -1][col] = VISITED; result.push([row - [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">let orangesRotting = function(grid) {

    const FRESH = 1,
          ROTTEN = 2,
          VISITED = -1;
    let rotten = [],
        totalFresh = 0,
        totalEmpty = 0,
        total = 0;

    function getAdjacents(row, col) {
        let result = [];
        // check top
        if (row > 0 &amp;&amp; grid[row -1][col] === FRESH) {
            grid[row -1][col] = VISITED;
            result.push([row - 1, col]);
        }

        // check bottom
        if (row &lt; grid.length - 1 &amp;&amp; grid[row + 1][col] === FRESH) {
            grid[row +1][col] = VISITED;
            result.push([row + 1, col]);
        }

        // check left;
        if (col > 0 &amp;&amp; grid[row][col - 1] === FRESH) {
            grid[row][col - 1] = VISITED;
            result.push([row, col - 1]);
        }

        // check right
        if (col &lt; grid[row].length - 1 &amp;&amp; grid[row][col + 1] === FRESH) {
            grid[row][col + 1] = VISITED;
            result.push([row, col + 1]);
        }

        return result;
    }

    for (let i = 0; i &lt; grid.length; i++) {
        for (let j = 0; j &lt; grid[i].length; j++) {
            const data = grid[i][j];
            if (data === ROTTEN) {
                rotten.push(...getAdjacents(i, j));
            }
            else if (data === FRESH || data === VISITED) {
                totalFresh++;
            }
            else {
                totalEmpty++;
            }
            total++;
        }
    }

    totalFresh -= rotten.length;
    let minute = 0;
    while (rotten.length) {
        let len = rotten.length;
        while (len --> 0) {
            let point = rotten.pop();
            let [row, col] = point;
            grid[row][col] = ROTTEN;
            rotten.unshift(...getAdjacents(row, col));
        }

        totalFresh -= rotten.length;
        minute++;
    }

    if (total === totalEmpty) return 0;

    let hasFreshOranges = totalFresh > 0;
    if (hasFreshOranges) return -1;

    return minute;
};</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/13/oranges-rotting/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">56</post-id>	</item>
		<item>
		<title>Level Order ZigZag Traversal</title>
		<link>https://jsalgorithm.com/2020/05/12/level-order-zigzag-traversal/</link>
					<comments>https://jsalgorithm.com/2020/05/12/level-order-zigzag-traversal/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Tue, 12 May 2020 19:50:55 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[Breadth First Search]]></category>
		<category><![CDATA[binary-tree]]></category>
		<category><![CDATA[breadth-first-search]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=52</guid>

					<description><![CDATA[function levelOrderZigzag(root) { let queue = [root], result = [], direction = 0; function process(node) { result.push(node.val); //? } while (queue.length) { let level = []; while(queue.length) { const node = queue.pop(); process(node); if (direction === 0) { if (node.left) level.push(node.left); if (node.right) level.push(node.right); } else { if (node.right) level.push(node.right); if (node.left) level.push(node.left); } } [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">function levelOrderZigzag(root) {

    let queue = [root], result = [], direction = 0;
    function process(node) {
        result.push(node.val); //?
    }

    while (queue.length) {
        let level = [];
        while(queue.length) {
            const node = queue.pop();
            process(node);
            if (direction === 0) {
                if (node.left) level.push(node.left);
                if (node.right) level.push(node.right);
            }
            else {
                if (node.right) level.push(node.right);
                if (node.left) level.push(node.left);
            }
        }
        direction ^= 1;
        queue = level;
    }

    return result; //?
}</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/12/level-order-zigzag-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">52</post-id>	</item>
		<item>
		<title>Level Order Tree Traversal</title>
		<link>https://jsalgorithm.com/2020/05/12/level-order-tree-traversal/</link>
					<comments>https://jsalgorithm.com/2020/05/12/level-order-tree-traversal/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Tue, 12 May 2020 19:28:31 +0000</pubDate>
				<category><![CDATA[Binary Tree]]></category>
		<category><![CDATA[Breadth First Search]]></category>
		<category><![CDATA[binary-tree]]></category>
		<category><![CDATA[breadth-first-search]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=50</guid>

					<description><![CDATA[function levelOrder(root) { let queue = [root], result = []; function process(node) { result.push(node.val); } while (queue.length) { const node = queue.pop(); process(node); if (node.left) queue.unshift(node.left); if (node.right) queue.unshift(node.right); } return result; //?}]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">function levelOrder(root) {<br><br>    let queue = [root], result = [];<br>    function process(node) {<br>        result.push(node.val);<br>    }<br><br>    while (queue.length) {<br>        const node = queue.pop();<br>        process(node);<br>        if (node.left) queue.unshift(node.left);<br>        if (node.right) queue.unshift(node.right);<br>    }<br><br>    return result; //?<br>}</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/12/level-order-tree-traversal/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">50</post-id>	</item>
		<item>
		<title>Number of Islands</title>
		<link>https://jsalgorithm.com/2020/05/11/number-of-islands/</link>
					<comments>https://jsalgorithm.com/2020/05/11/number-of-islands/#respond</comments>
		
		<dc:creator><![CDATA[Ronald]]></dc:creator>
		<pubDate>Mon, 11 May 2020 21:09:31 +0000</pubDate>
				<category><![CDATA[Breadth First Search]]></category>
		<category><![CDATA[Leetcode]]></category>
		<category><![CDATA[Recursion]]></category>
		<category><![CDATA[breadth-first-search]]></category>
		<category><![CDATA[leetcode]]></category>
		<category><![CDATA[recursion]]></category>
		<guid isPermaLink="false">http://50.19.13.106/?p=25</guid>

					<description><![CDATA[let numIslands = function(matrix) { if (!matrix.length) return 0; let land = 1, visited = 'X', counter = 0, maxRow = matrix.length - 1, maxCol = matrix[0].length - 1; function exploreAndMark(row, col) { if (row &#60; 0 &#124;&#124; col &#60; 0 &#124;&#124; row > maxRow &#124;&#124; col > maxCol) { return; } if (matrix[row][col] === [&#8230;]]]></description>
										<content:encoded><![CDATA[
<pre class="wp-block-preformatted">let numIslands = function(matrix) {

    if (!matrix.length) return 0;

    let land = 1, visited = 'X',
        counter = 0,
        maxRow = matrix.length - 1,
        maxCol = matrix[0].length - 1;

    function exploreAndMark(row, col) {
        if (row &lt; 0 || col &lt; 0 || row > maxRow || col > maxCol) {
            return;
        }

        if (matrix[row][col] === land) {
            matrix[row][col] = visited;

            // explore up
            exploreAndMark(row - 1, col);

            // explore down
            exploreAndMark(row + 1, col);

            // explore left
            exploreAndMark(row, col - 1);

            // explore right
            exploreAndMark(row, col + 1);
        }
    }

    for (let row = 0; row &lt;= maxRow; row++) {
        for (let col = 0; col &lt;= maxCol; col++) {
            if (matrix[row][col] === land) {
                counter++;
                exploreAndMark(row, col);
            }
        }
    }

    return counter;
}</pre>
]]></content:encoded>
					
					<wfw:commentRss>https://jsalgorithm.com/2020/05/11/number-of-islands/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">25</post-id>	</item>
	</channel>
</rss>
