private static int[] Helper(LeetCode549TreeNode root) { if (root == null) { return new int[] { 0, 0 } } ; var inr = 1; var dcr = 1; if (root.left != null) { var l = Helper(root.left); if (root.val == root.left.val + 1) { dcr = l[1] + 1; } else if (root.val == root.left.val - 1) { inr = l[0] + 1; } } if (root.right != null) { var r = Helper(root.right); if (root.val == root.right.val + 1) { dcr = Math.Max(dcr, r[1] + 1); } else if (root.val == root.right.val - 1) { inr = Math.Max(inr, r[0] + 1); } } _maxVal = Math.Max(_maxVal, dcr + inr - 1); return(new int[] { inr, dcr }); } }
public static int LongestConsecutive(LeetCode549TreeNode root) { return(_maxVal); }
public LeetCode549TreeNode(int val = 0, LeetCode549TreeNode left = null, LeetCode549TreeNode right = null) { this.val = val; this.left = left; this.right = right; }