zoukankan      html  css  js  c++  java
  • lintcode595- Binary Tree Longest Consecutive Sequence- easy

    Given a binary tree, find the length of the longest consecutive sequence path.

    The path refers to any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The longest consecutive path need to be from parent to child (cannot be the reverse).

    Example

    For example,

       1
        
         3
        / 
       2   4
            
             5
    

    Longest consecutive sequence path is 3-4-5, so return 3.

       2
        
         3
        / 
       2    
      / 
     1
    

    Longest consecutive sequence path is 2-3,not3-2-1, so return 2.

    它这里的consecutive意思是数字一定要连续+1.

    用分治法 + 全局变量监控。左边增加长度要求root.val + 1 == root.left.val 。同时要注意如果某个点发生了断层,要重新开始计数。

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    
    
    public class Solution {
        /*
         * @param root: the root of binary tree
         * @return: the length of the longest consecutive sequence path
         */
         
        private int maxConsec = Integer.MIN_VALUE;
        
        public int longestConsecutive(TreeNode root) {
            // write your code here
            helper(root);
            return maxConsec;
        }
        
        private int helper(TreeNode root) {
            if (root == null) {
                return 0;
            }
            
            int left = helper(root.left);
            int right = helper(root.right);
            
            int crtLeft = Integer.MIN_VALUE;
            int crtRight = Integer.MIN_VALUE;
    
            if (root.left != null && root.val + 1 == root.left.val) {
                crtLeft = 1 + left;
            } 
            // 注意这里的处理。如果发生断层不连续了,这条路往上就不能用之前的继续计数了。
            else {
                crtLeft = 1;
            }
            
            if (root.right != null && root.val + 1 == root.right.val) {
                crtRight = 1 + right;
            } else {
                crtRight = 1;
            }
            
            int crtMax = Math.max(crtLeft, crtRight);
            if (crtMax > maxConsec) {
                maxConsec = crtMax;
            }
            return crtMax;
        }
    }

    九章算法的:

    想到除了null别的点起码都是1,从而节省了代码空间。

    // version 2: Another Traverse + Divide Conquer 
    public class Solution {
        private int longest;
        
        /**
         * @param root the root of binary tree
         * @return the length of the longest consecutive sequence path
         */
        public int longestConsecutive(TreeNode root) {
            longest = 0;
            helper(root);
            return longest;
        }
        
        private int helper(TreeNode root) {
            if (root == null) {
                return 0;
            }
            
            int left = helper(root.left);
            int right = helper(root.right);
            
            int subtreeLongest = 1; // at least we have root
            if (root.left != null && root.val + 1 == root.left.val) {
                subtreeLongest = Math.max(subtreeLongest, left + 1);
            }
            if (root.right != null && root.val + 1 == root.right.val) {
                subtreeLongest = Math.max(subtreeLongest, right + 1);
            }
            
            if (subtreeLongest > longest) {
                longest = subtreeLongest;
            }
            return subtreeLongest;
        }
    }
  • 相关阅读:
    centos7安装apache http server启动失败--Failed to start The Apache HTTP Server.
    Centos之文件搜索命令find
    OpenSSL Heartbleed “心脏滴血”漏洞简单攻击示例
    nginx解析漏洞简介
    图片写入一句话木马
    cobaltstrike派生一个shell给metasploit
    Linux下date命令、格式化输出、时间设置
    msfvenom 常用生成 payload 命令
    metasploit派生一个shell给cobaltstrike
    centos 7 lsof简介
  • 原文地址:https://www.cnblogs.com/jasminemzy/p/7659178.html
Copyright © 2011-2022 走看看