Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
657 B
2
Indexable
Never
class Solution {
 
    public int longestZigZag(TreeNode root) {
        if(root == null)
            return 0;
        
        return Math.max(traverse(root.left, true, 1), traverse(root.right, false, 1));
    }

    public int traverse(TreeNode t, boolean isLeft, int length){ 
        if(t == null){
            return length-1;
        }
        
        if(isLeft){
            return Math.max( traverse(t.left, true, 1), traverse(t.right, false, length+1) );
        } 
        else{
            return Math.max( traverse(t.left, true, length+1), traverse(t.right, false, 1) );
        }
        
        
    }    
        
}