admin管理员组文章数量:1531705
1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val = x; } 8 * } 9 */ 10 public class Solution { 11 private int result = 1; 12 public int longestConsecutive(TreeNode root) { 13 if (root == null) { 14 return 0; 15 } 16 getLongest(root, Integer.MIN_VALUE, 0); 17 return result; 18 } 19 20 private void getLongest(TreeNode root, int target, int count) { 21 if (root == null) { 22 result = Math.max(result, count); 23 return; 24 } 25 26 if (root.val == target + 1) { 27 count++; 28 result = Math.max(result, count); 29 } else { 30 count = 1; 31 } 32 getLongest(root.left, root.val, count); 33 getLongest(root.right, root.val, count); 34 } 35 }
Maximum value get should be count increase. Otherwise, it already breaks the consequences.
转载于:https://wwwblogs/shuashuashua/p/5645615.html
本文标签: treebinaryconsequenceLongest
版权声明:本文标题:Binary Tree Longest Consequence 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1726825914a1086073.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论