Untitled
unknown
java
4 years ago
965 B
10
Indexable
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public int longestZigZag(TreeNode root) {
if(root==null)
{
return 0;
}
return Math.max(1+helper(root.left,'l'),1+helper(root.right,'r'));
}
public int helper(TreeNode root,char c)
{
if(root==null)
{
return -1;
}
if(c=='l')
{
return Math.max(1+helper(root.right,'r'),0+helper(root.left,'l'));
}
else
{
return Math.max(1+helper(root.left,'l'),0+helper(root.right,'r'));
}
}
}Editor is loading...