美文网首页
Binary Tree Longest Consecutive

Binary Tree Longest Consecutive

作者: stepsma | 来源:发表于2017-04-10 02:49 被阅读0次

    不得不说,Tree的题还算挺有意思的。首先给出two pass的做法: pass one找往下的sequence,存到map里;然后 pass two找往上的sequence,再更新全局变量.

    class Solution {
    public:
    
        int search_smaller(TreeNode *root){
            if(!root) return 0;
            int left = search_smaller(root->left);
            int right = search_smaller(root->right);
            int cur = 1;
            if(root->left && root->left->val == root->val - 1){
                cur = max(cur, left + 1); 
            }
            if(root->right && root->right->val == root->val - 1){
                cur = max(cur, right + 1);
            }
            mp[root] = cur;
            return cur;
        }
        
        void search_bigger(TreeNode *root){
            if(!root) return;
            max_count = max(max_count, mp[root]);
            if(root->left && root->left->val == root->val + 1){
                mp[root->left] = max(mp[root->left], mp[root] + 1);
            }
            if(root->right && root->right->val == root->val + 1){
                mp[root->right] = max(mp[root->right], mp[root] + 1);
            }
            search_bigger(root->left);
            search_bigger(root->right);
        }
        
        int longestConsecutive(TreeNode* root) {
            if(!root) return 0;
            search_smaller(root);
            search_bigger(root);
            return max_count;
        }
    private:
        unordered_map<TreeNode*, int> mp;
        int max_count = 1;
    };
    

    网上果然有one pass的解法,就是把同一个节点的增序数目,和降序数目统计到一起,然后该节点序列为 增序 + 降序 - 1,即可

    class Solution {
        
       struct ResultSet{
            int dec, inc;
            ResultSet(int d, int i) : dec(d), inc(i){}
       };
        
    public:
    
        ResultSet search_util(TreeNode* root){
            if(!root) return ResultSet(0, 0);
            ResultSet left = search_util(root->left);
            ResultSet right = search_util(root->right);
            ResultSet ret(1, 1);
            if(root->left){
                if(root->left->val == root->val - 1){
                    ret.inc = max(ret.inc, left.inc + 1);
                }else if(root->left->val == root->val + 1){
                    ret.dec = max(ret.dec, left.dec + 1);
                }
            }
            if(root->right){
               if(root->right->val == root->val - 1){
                    ret.inc = max(ret.inc, right.inc + 1);
                }else if(root->right->val == root->val + 1){
                    ret.dec = max(ret.dec, right.dec + 1);
                } 
            }
            max_count = max(max_count, ret.inc + ret.dec - 1);
            return ret;
        }
    
        int longestConsecutive(TreeNode* root) {
            if(!root) return 0;
            search_util(root);
            return max_count;
        }
    private:
        int max_count = 1;
    };
    

    相关文章

      网友评论

          本文标题:Binary Tree Longest Consecutive

          本文链接:https://www.haomeiwen.com/subject/nlgjattx.html