Binary Tree Maximum Path Sum, non-recursive, Time Limit Exceeded
<p>I'm struggling with this problem, which I want to solve in non-recursive way. There seems no logic error in my algorithm, 73% test cases passed. But it can't handle the big data, reported "Time Limit Exceeded". I appreciate if somebody could give me some hint how to do it in non-recursive and avoid time limit exceed, thanks in advance!</p>

<p><strong>Problem Link</strong></p>

<blockquote>
  <p>I believe there's also a similar one in LeetCode.</p>
  
  <p><a href="http://www.lintcode.com/en/problem/binary-tree-maximum-path-sum-ii/" rel="nofollow">http://www.lintcode.com/en/problem/binary-tree-maximum-path-sum-ii/</a></p>
</blockquote>

<p><strong>Problem description:</strong></p>

<blockquote>
  <p>Given a binary tree, find the maximum path sum from root.
  The path may end at any node in the tree and contain at least one node in it.</p>
</blockquote>

<p><strong>Example:</strong></p>

<blockquote>
  <p>Given the below binary tree:</p>
  
  <p>1</p>
  
  <p>/ \</p>
  
  <p>2   3</p>
</blockquote>

<p>return 4. (1->3)</p>

<p><strong>Judge</strong></p>

<blockquote>
  <p>Time Limit Exceeded</p>
  
  <p>Total Runtime: 1030 ms</p>
</blockquote>

<p><strong>Input</strong>
Input Data</p>

<blockquote>
  <p>{-790,-726,970,696,-266,-545,830,-866,669,-488,-122,260,116,521,-866,-480,-573,-926,88,733,#,#,483,-935,-285,-258,892,180,279,-935,675,2,596,5,50,830,-607,-212,663,25,-840,#,#,-333,754,#,817,842,-220,-269,9,-862,-78,-473,643,536,-142,773,485,262,360,702,-661,244,-96,#,519,566,-893,-599,126,-314,160,358,159,#,#,-237,-522,-327,310,-506,462,-705,868,-782,300,-945,-3,139,-193,-205,-92,795,-99,-983,-658,-114,-706,987,292,#,234,-406,-993,-863,859,875,383,-729,-748,-258,329,431,-188,-375,-696,-856,825,-154,-398,-917,-70,105,819,-264,993,207,21,-102,50,569,-824,-604,895,-564,-361,110,-965,-11,557,#,202,213,-141,759,214,207,135,329,15,#,#,244,#,334,628,509,627,-737,-33,-339,-985,349,267,-505,-527,882,-352,-357,-630,782,-215,-555,132,-835,-421,751,0,-792,-575,-615,-690,718,248,882,-606,-53,157,750,862,#,940,160,47,-347,-101,-947,739,894,#,-658,-90,-277,-925,997,862,-481,-83,708,706,686,-542,485,517,-922,978,-464,-923,710,-691,168,-607,-888,-439,499,794,-601,435,-114,-337,422,#,-855,-859,163,-224,902,#,577,#,-386,272,-9 ...</p>
</blockquote>

<p><strong>Expected</strong></p>

<blockquote>
  <p>6678</p>
</blockquote>

<p><strong>My Code</strong>
C++</p>

<pre><code>/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this-&gt;val = val;
 *         this-&gt;left = this-&gt;right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param root the root of binary tree.
     * @return an integer
     */
    int maxPathSum2(TreeNode *root) {
        if (root == NULL) return 0;
        findLeaf(root);
        return global_max;
    }

private:
    int global_max = INT_MIN;

    void findLeaf(TreeNode* root) {
        unordered_map&lt;TreeNode*, TreeNode*&gt; parent;
        stack&lt;TreeNode*&gt; traverse;
        parent[root] = NULL;
        traverse.push(root);

        while(!traverse.empty()) {
            TreeNode* p = traverse.top();
            traverse.pop();
            if (!p-&gt;left &amp;&amp; !p-&gt;right) {
                findPathMaxSum(p, parent);
            }
            if (p-&gt;right) {
                parent[p-&gt;right] = p;
                traverse.push(p-&gt;right);
            }
            if (p-&gt;left) {
                parent[p-&gt;left] = p;
                traverse.push(p-&gt;left);
            }
        }
    }

    void findPathMaxSum(TreeNode* leaf, unordered_map&lt;TreeNode*, TreeNode*&gt; parent) {
        TreeNode* current = leaf;
        stack&lt;TreeNode*&gt; stk;
        int path_max = INT_MIN;
        int path_sum = 0;

        while (current) {
            stk.push(current);
            current = parent[current];
        }

        while (!stk.empty()) {
            current = stk.top();
            stk.pop();
            path_sum += current-&gt;val;
            path_max = path_max &gt; path_sum ? path_max : path_sum;
        }

        global_max = global_max &gt; path_max ? global_max : path_max;
    }
};
</code></pre>

<p><strong>SOLVED</strong></p>

<p>I accept the advice by @Dave Galvin and it works! Here's the code:</p>

<pre><code>/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this-&gt;val = val;
 *         this-&gt;left = this-&gt;right = NULL;
 *     }
 * }
 */
class Solution {
public:
    /**
     * @param root the root of binary tree.
     * @return an integer
     */
    int maxPathSum2(TreeNode *root) {
        if (root == NULL) return 0;
        int global_max = INT_MIN;
        stack&lt;TreeNode*&gt; traverse;
        traverse.push(root);
        while(!traverse.empty()) {
            TreeNode* p = traverse.top();
            global_max = global_max &gt; p-&gt;val ? global_max : p-&gt;val;
            traverse.pop();
            if (p-&gt;right) {
                traverse.push(p-&gt;right);
                p-&gt;right-&gt;val += p-&gt;val;
            }
            if (p-&gt;left) {
                traverse.push(p-&gt;left);
                p-&gt;left-&gt;val += p-&gt;val;
            }
        }
        return global_max;
    }
};
</code></pre>