二叉树最大路径总和,非递归,超出时间限制

时间:2016-09-24 14:23:33

标签: c++ algorithm binary-tree non-recursive

我正在努力解决这个问题,我想以非递归的方式解决这个问题。我的算法似乎没有逻辑错误,73%的测试用例通过了。但它无法处理大数据,报告称“超出时间限制”。我很感激,如果有人能给我一些暗示如何以非递归的方式做到这一点并避免时间限制超过,请提前感谢!

问题链接

  

我相信在LeetCode中也有类似的。

     

http://www.lintcode.com/en/problem/binary-tree-maximum-path-sum-ii/

问题描述:

  

给定二叉树,从根中找到最大路径总和。   路径可以在树中的任何节点处结束,并且其中至少包含一个节点。

示例:

  

鉴于以下二叉树:

     

1

     

/ \

     

2 3

返回4.(1-> 3)

法官

  

超出时间限制

     

总运行时间:1030毫秒

输入 输入数据

  

{ - 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,#,#, - 333754,#817842,-220,-269,9,-862,-78, - 473643536,-142,773,485,262,360,702,-661,244,-96,#519566,-893,-599,126,-314,160,358,159,#,#, - 237,-522,-327,310,-506,462,-705,868,-782,300,-945, - 3139,-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, #202213,-141,759,214,207,135,329,15,#,#,244#,334628509627,-737,-33,-339,-985,349,267,-505,-527,882,-352,-357,-630,782,-215, - 555132,-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 ......

预期

  

6678

我的代码 C ++

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->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<TreeNode*, TreeNode*> parent;
        stack<TreeNode*> traverse;
        parent[root] = NULL;
        traverse.push(root);

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

    void findPathMaxSum(TreeNode* leaf, unordered_map<TreeNode*, TreeNode*> parent) {
        TreeNode* current = leaf;
        stack<TreeNode*> 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->val;
            path_max = path_max > path_sum ? path_max : path_sum;
        }

        global_max = global_max > path_max ? global_max : path_max;
    }
};

解决

我接受@Dave Galvin的建议并且它有效!这是代码:

/**
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->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<TreeNode*> traverse;
        traverse.push(root);
        while(!traverse.empty()) {
            TreeNode* p = traverse.top();
            global_max = global_max > p->val ? global_max : p->val;
            traverse.pop();
            if (p->right) {
                traverse.push(p->right);
                p->right->val += p->val;
            }
            if (p->left) {
                traverse.push(p->left);
                p->left->val += p->val;
            }
        }
        return global_max;
    }
};

3 个答案:

答案 0 :(得分:1)

我猜您的代码存在的问题是,当您遍历树时,在每个节点中,您都在迭代以计算最大路径。这最终会导致uint8 float64 的复杂性。您需要计算流上的最大路径(在遍历树时)。

在下面的解决方案中,我使用了来自here的后序迭代算法。请原谅我用过这个而不是你用的。

解决方案(O(n^2))只是向每个节点添加一个字段O(n),当访问时,实际节点在max_path之间取最大值left

right

答案 1 :(得分:0)

<强>编辑:

您不需要 findPathMaxSum 功能。我还更改了地图。现在它存储了2个值:

  1. 指向父母的指针
  2. 从根到当前节点的路径总和。
  3. 这是代码。

    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<TreeNode*, std::pair<TreeNode*,int> > parent;
            stack<TreeNode*> traverse;
            parent[root] = make_pair(NULL,root->val);
            traverse.push(root);
    
            while(!traverse.empty()) {
                TreeNode* p = traverse.top();
                traverse.pop();
                if (!p->left && !p->right) {
                    // findPathMaxSum(p, parent);
                    global_max=std::max(global_max,parent[p].second);
                }
                if (p->right) {
                    parent[p->right] = make_pair(p, (p->right->val) +parent[p].second) ;
                    traverse.push(p->right);
                }
                if (p->left) {
                    parent[p->left] = make_pair(p, (p->left->val) +parent[p].second) ;
                    traverse.push(p->left);
                }
            }
        };
    

    <强> OLD:

    您想通过 findPathMaxSum 中的引用而不是值传递地图。

    void findPathMaxSum(TreeNode* leaf, unordered_map<TreeNode*, TreeNode*> parent)
    

    将其更改为。

    void findPathMaxSum(TreeNode* leaf, unordered_map<TreeNode*, TreeNode*>& parent)
    

    它使你的时间复杂度为O(n * n)。

    它的运行复杂性将变为O(n * log n)。虽然它没有成功,因为你的约束更严格。所以我在上面发布了一个O(n)解决方案。

答案 2 :(得分:0)

从上到下,通过向其添加父级值来更新每个节点。跟踪您的最大值及其位置。最后回来。为O(n)。

例如,如果您的二叉树是T = [ - 4,2,6,-5,2,1,5],那么我们将其更新为: [-4,2-4 = -2,6-4 = 2,-2-5 = -7,-2 + 2 = 4,2 + 3 = 3 + 2 + 5 = 7]

答案是7,即-4,6,5。