Consecutive Tree Sequence

来源:互联网 发布:一个直播软件多少钱 编辑:程序博客网 时间:2024/06/05 04:26

 Binary Tree Longest Consecutive Sequence

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).

  1    \     3    / \   2   4        \         5
   2    \     3    /    2      /  1

/** * 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     */    /*    int max = 0;    public int longestConsecutive(TreeNode root) {        // Write your code here        if (root == null)            return 0;        helper(root, 0, root.val); //0代表curLen, 开始递归时,root 还未处理        return max;    }        private void helper(TreeNode root, int curLen, int target){        //preorder traversal        if (root == null){            return;        }         if (root.val == target){            curLen++;        } else {            curLen = 1;        }        max = Math.max(max, curLen);        helper(root.left, curLen, root.val + 1);        helper(root.right, curLen, root.val + 1);    }    */        //postOrder traversal    /*    private int longest = 0;    public int longestConsecutive(TreeNode root) {        helper(root);        return longest;    }    private int helper(TreeNode root){        //base case        if (root == null){            return 0;        }        int curLen = 1;  //at least there is root        //divide        int left = helper(root.left);        int right = helper(root.right);                //conquer        if (root.left != null && root.val + 1 == root.left.val){            curLen = left + 1;        }        if (root.right != null && root.val + 1 == root.right.val){            curLen = Math.max(curLen, right + 1); //need to get the longer path from right and left        }        //otherwise longest will 1 to pass on to the upper level        if (curLen > longest){            longest = curLen;        }        return curLen;    } */        private class ResultType{        int curLen; // the curLen in this node as ending node, traverse from bottom to top        int maxLen; // in the subtree and itself        public ResultType(int curLen, int maxLen){            this.curLen = curLen;            this.maxLen = maxLen;        }    }    //how to avoid global variables, using postOrder traversal    public int longestConsecutive(TreeNode root) {        ResultType res = helper(root);        return res.maxLen;    }        private ResultType helper(TreeNode root){        if (root == null){            return new ResultType(0, 0);        }        ResultType left = helper(root.left);        ResultType right = helper(root.right);        //how to update the curLen and maxLen        int curLen = 1;        if (root.left != null && root.val + 1 == root.left.val){            curLen = 1 + left.curLen;        }        if (root.right != null && root.val + 1 == root.right.val){            curLen = Math.max(curLen, 1 + right.curLen);        }        int maxLen = Math.max(curLen, Math.max(left.maxLen, right.maxLen));        return new ResultType(curLen, maxLen);    }}

0 0