|
| 1 | +/** |
| 2 | + * Given a binary tree, find the length of the longest consecutive sequence path. |
| 3 | + * |
| 4 | + * The path refers to any sequence of nodes from some starting node to any node |
| 5 | + * in the tree along the parent-child connections. The longest consecutive path |
| 6 | + * need to be from parent to child (cannot be the reverse). |
| 7 | + * |
| 8 | + * Example 1: |
| 9 | + * |
| 10 | + * Input: |
| 11 | + * |
| 12 | + * 1 |
| 13 | + * \ |
| 14 | + * 3 |
| 15 | + * / \ |
| 16 | + * 2 4 |
| 17 | + * \ |
| 18 | + * 5 |
| 19 | + * |
| 20 | + * Output: 3 |
| 21 | + * Explanation: Longest consecutive sequence path is 3-4-5, so return 3. |
| 22 | + * |
| 23 | + * Example 2: |
| 24 | + * |
| 25 | + * Input: |
| 26 | + * |
| 27 | + * 2 |
| 28 | + * \ |
| 29 | + * 3 |
| 30 | + * / |
| 31 | + * 2 |
| 32 | + * / |
| 33 | + * 1 |
| 34 | + * |
| 35 | + * Output: 2 |
| 36 | + * Explanation: Longest consecutive sequence path is 2-3, not 3-2-1, so return 2. |
| 37 | + */ |
| 38 | + |
| 39 | +/** |
| 40 | + * Definition for a binary tree node. |
| 41 | + * public class TreeNode { |
| 42 | + * int val; |
| 43 | + * TreeNode left; |
| 44 | + * TreeNode right; |
| 45 | + * TreeNode(int x) { val = x; } |
| 46 | + * } |
| 47 | + */ |
| 48 | + |
| 49 | +public class BinaryTreeLongestConsecutiveSequence298 { |
| 50 | + public int longestConsecutive(TreeNode root) { |
| 51 | + if (root == null) return 0; |
| 52 | + return helper(root, 0); |
| 53 | + } |
| 54 | + |
| 55 | + public int helper(TreeNode root, int i) { |
| 56 | + if (root == null) return i; |
| 57 | + |
| 58 | + int l = i + 1; |
| 59 | + if (root.left != null) { |
| 60 | + if (root.val + 1 == root.left.val) { |
| 61 | + l = helper(root.left, i+1); |
| 62 | + } else { |
| 63 | + l = helper(root.left, 0); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + int r = i + 1; |
| 68 | + if (root.right != null) { |
| 69 | + if (root.val + 1 == root.right.val) { |
| 70 | + r = helper(root.right, i+1); |
| 71 | + } else { |
| 72 | + r = helper(root.right, 0); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return Math.max(Math.max(l, r), i+1); |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /** |
| 81 | + * Bottom-up |
| 82 | + * https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/solution/ |
| 83 | + */ |
| 84 | + private int maxLength = 0; |
| 85 | + public int longestConsecutive2(TreeNode root) { |
| 86 | + dfs(root); |
| 87 | + return maxLength; |
| 88 | + } |
| 89 | + |
| 90 | + private int dfs(TreeNode p) { |
| 91 | + if (p == null) return 0; |
| 92 | + int L = dfs(p.left) + 1; |
| 93 | + int R = dfs(p.right) + 1; |
| 94 | + if (p.left != null && p.val + 1 != p.left.val) { |
| 95 | + L = 1; |
| 96 | + } |
| 97 | + if (p.right != null && p.val + 1 != p.right.val) { |
| 98 | + R = 1; |
| 99 | + } |
| 100 | + int length = Math.max(L, R); |
| 101 | + maxLength = Math.max(maxLength, length); |
| 102 | + return length; |
| 103 | + } |
| 104 | + |
| 105 | +} |
0 commit comments