Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thanlau/6e326e835f07155640886269c2b5469b to your computer and use it in GitHub Desktop.
Save thanlau/6e326e835f07155640886269c2b5469b to your computer and use it in GitHub Desktop.
我的错误解法
1
/ \
2 0
/ \
3 1
这里helper(root.left)他意思是从根节点一直往下走。那么例如1过了,到了2,
第一,二个if我是找左子树往上走和往下走和往上走的最长路径,这里helper(root.left)他意思是从根节点一直往下走。那么例如1过了,到了2,
root.left.val + 1 ==root.val,root.left.val - 1 ==root.val 是看2的左子树是递增还是递减,在这里是递增,因此,root.left.val - 1 ==root.val成立
2的右子树正好递减,因此root.right.val+1 = root.val成立,但是我在例如第一个判断出来递减的时候
if(root.left !=null && root.left.val - 1 ==root.val){
up = Math.max(left + 1, up);}
我更新的是up = Math.max(left + 1, up),其中left = helper(root.left),相当于我不管递增还是递减,所有左子树深度我都+1
我们再来看你return helper函数的的值是:int curlength = up + down + 1;
那么我们为什么给left + 1 或者 right + 1 呢?left 和 right 应该就是返回前的curlength = up + down + 1
反问一下: curlenght +1 代表什么?
错误解法========================================================
public class Solution {
/**
* @param root the root of binary tree
* @return the length of the longest consecutive sequence path
*/
private int length = Integer.MIN_VALUE;
public int longestConsecutive2(TreeNode root) {
if( root == null){
return 0;
}
helper(root);
return length;
}
private int helper(TreeNode root){
if(root == null){
return 0;
}
//分别遍历左右子树,得到当前节点的左右子树长度
int left = helper(root.left);
int right = helper(root.right);
//记录当前深度
int up = 0;
int down = 0;
//节点往左子树走,只要满足当前节点+1等于左子树节点的值就继续走
//移动一次就加1,值更新给现在的左子树长度
if(root.left !=null && root.left.val + 1 ==root.val ){
down = Math.max(left + 1, down);
}
if(root.left !=null && root.left.val - 1 ==root.val){
up = Math.max(left + 1, up);
}
if(root.right !=null && root.right.val + 1 == root.val){
down = Math.max(right + 1, down);
}
if(root.right !=null && root.right.val - 1 ==root.val){
up = Math.max(right + 1, up);
}
int curlength = up + down + 1;
if(curlength > length){
length = curlength;
}
return curlength;
}
}
正确解法========================================================
/**
* 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
*/
class ResultType{
public int length, up, down;
public ResultType(int length, int up, int down){
this.length = length;
this.up = up;
this.down = down;
}
}
public int longestConsecutive2(TreeNode root) {
if( root == null){
return 0;
}
ResultType result = helper(root);
return result.length;
}
private ResultType helper(TreeNode root){
if(root == null){
return new ResultType(0,0,0);
}
ResultType left = helper(root.left);
ResultType right = helper(root.right);
int up = 0;
int down = 0;
//只要左子树没有走到底且此时递增
if(root.left != null && root.left.val - 1 == root.val){
up = Math.max(left.up + 1, up);
}
if(root.left != null && root.left.val + 1 == root.val){
down = Math.max(left.down + 1, down);
}
//处理右子树
if(root.right != null && root.right.val - 1 == root.val){
up = Math.max(right.up + 1, up);
}
if(root.right != null && root.right.val + 1 == root.val){
down = Math.max(right.down + 1, down);
}
int length = up + down + 1;//经过root的最大深度
//和没经过root的左右子树的最大深度
length = Math.max(length, Math.max(left.length,right.length));
return new ResultType(length,up,down);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment