<p>A <strong>path</strong> in a binary tree is a sequence of nodes where each pair of adjacent nodes in the sequence has an edge connecting them. A node can only appear in the sequence <strong>at most once</strong>. Note that the path does not need to pass through the root.</p>

<p>The <strong>path sum</strong> of a path is the sum of the node&#39;s values in the path.</p>

<p>Given the <code>root</code> of a binary tree, return <em>the maximum <strong>path sum</strong> of any <strong>non-empty</strong> path</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/13/exx1.jpg" style="width: 322px; height: 182px;" />
<pre>
<strong>Input:</strong> root = [1,2,3]
<strong>Output:</strong> 6
<strong>Explanation:</strong> The optimal path is 2 -&gt; 1 -&gt; 3 with a path sum of 2 + 1 + 3 = 6.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2020/10/13/exx2.jpg" />
<pre>
<strong>Input:</strong> root = [-10,9,20,null,null,15,7]
<strong>Output:</strong> 42
<strong>Explanation:</strong> The optimal path is 15 -&gt; 20 -&gt; 7 with a path sum of 15 + 20 + 7 = 42.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the tree is in the range <code>[1, 3 * 10<sup>4</sup>]</code>.</li>
	<li><code>-1000 &lt;= Node.val &lt;= 1000</code></li>
</ul>

#pragma GCC optimize("Ofast")
static auto _ = [](){ cin.tie(nullptr) -> ios_base::sync_with_stdio(false); return nullptr; }();
#define endl "\n"

class Solution {
public:
    int maxPathSum(TreeNode* root) {
        // find lowest node
        // go to parent node
        // -- check if left node has children
        // -- check if right node has children
        // remake parent's value to max of {value, value+value_left, value+value_right}
        // set maxPath to max of {new parent value, sum all nodes}

        int maxPath = root -> val;
        TreeNode* curr_node = root;

        vector<TreeNode*> queue;
        
        while(true){
            TreeNode* left = nullptr;
            TreeNode* right = nullptr;
            int val = curr_node -> val;
            int left_val = 0, right_val = 0;
            cout << val << endl;

            if(curr_node -> left != nullptr){
                left = curr_node -> left;
                left_val = left -> val;
                if(left -> left != nullptr || left -> right != nullptr){
                    queue.emplace_back(curr_node);
                    curr_node = left;
                    continue;
                }
                maxPath = max(maxPath, left_val);
            }
            if(curr_node -> right != nullptr){
                right = curr_node -> right;
                right_val = right -> val;
                if(right -> left != nullptr || right -> right != nullptr){
                    queue.emplace_back(curr_node);
                    curr_node = right;
                    continue;
                }
                maxPath = max(maxPath, right_val);
            }

            curr_node -> left = nullptr;
            curr_node -> right = nullptr;

            curr_node -> val = max(val, val + max(left_val, right_val));
            maxPath = max(maxPath, curr_node -> val);
            maxPath = max(maxPath, max(val, val + left_val + right_val));

            cout << (curr_node -> val) << endl;
            
            if(queue.size() == 0) break;

            curr_node = queue.back();
            queue.pop_back();
        }

        return maxPath;
    }
};
20500