A binary tree is named Even-Odd if it meets the following conditions:
The root of the binary tree is at level index 0, its children are at level index 1, their children are at level index 2, etc.
For every even-indexed level, all nodes at the level have odd integer values in strictly increasing order (from left to right).
For every odd-indexed level, all nodes at the level have even integer values in strictly decreasing order (from left to right).
Given the root of a binary tree, return true if the binary tree is Even-Odd, otherwise return false.
Example 1:
Input: root = [1,10,4,3,null,7,9,12,8,6,null,null,2]
Output: true
Explanation: The node values on each level are:
Level 0: [1]
Level 1: [10,4]
Level 2: [3,7,9]
Level 3: [12,8,6,2]
Since levels 0 and 2 are all odd and increasing and levels 1 and 3 are all even and decreasing, the tree is Even-Odd.
Example 2:
Input: root = [5,4,2,3,3,7]
Output: false
Explanation: The node values on each level are:
Level 0: [5]
Level 1: [4,2]
Level 2: [3,3,7]
Node values in level 2 must be in strictly increasing order, so the tree is not Even-Odd.
Example 3:
Input: root = [5,9,1,3,5,7]
Output: false
Explanation: Node values in the level 1 should be even integers.
Constraints:
The number of nodes in the tree is in the range [1, 105].
1 <= Node.val <= 106
Solutions
Solution 1: BFS
BFS traverses level by level. Each level is judged by its parity. The node values at each level are either all even or all odd, and they are strictly increasing or decreasing.
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree.
1 2 3 4 5 6 7 8 910111213141516171819202122232425
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclassSolution:defisEvenOddTree(self,root:Optional[TreeNode])->bool:even=1q=deque([root])whileq:prev=0ifevenelseinffor_inrange(len(q)):root=q.popleft()ifevenand(root.val%2==0orprev>=root.val):returnFalseifnotevenand(root.val%2==1orprev<=root.val):returnFalseprev=root.valifroot.left:q.append(root.left)ifroot.right:q.append(root.right)even^=1returnTrue
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcisEvenOddTree(root*TreeNode)bool{even:=trueq:=[]*TreeNode{root}forlen(q)>0{varprevint=1e7ifeven{prev=0}forn:=len(q);n>0;n--{root=q[0]q=q[1:]ifeven&&(root.Val%2==0||prev>=root.Val){returnfalse}if!even&&(root.Val%2==1||prev<=root.Val){returnfalse}prev=root.Valifroot.Left!=nil{q=append(q,root.Left)}ifroot.Right!=nil{q=append(q,root.Right)}}even=!even}returntrue}
Solution 2: DFS
DFS performs a pre-order traversal of the binary tree, and similarly judges whether it meets the conditions based on the parity of the layer where the node is located. During the traversal, a hash table is used to record the node value that was most recently visited at each layer.
The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes in the binary tree.
1 2 3 4 5 6 7 8 910111213141516171819202122
# Definition for a binary tree node.# class TreeNode:# def __init__(self, val=0, left=None, right=None):# self.val = val# self.left = left# self.right = rightclassSolution:defisEvenOddTree(self,root:Optional[TreeNode])->bool:defdfs(root,i):ifrootisNone:returnTrueeven=i%2==0prev=d.get(i,0ifevenelseinf)ifevenand(root.val%2==0orprev>=root.val):returnFalseifnotevenand(root.val%2==1orprev<=root.val):returnFalsed[i]=root.valreturndfs(root.left,i+1)anddfs(root.right,i+1)d={}returndfs(root,0)
/** * Definition for a binary tree node. * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */funcisEvenOddTree(root*TreeNode)bool{d:=map[int]int{}vardfsfunc(*TreeNode,int)booldfs=func(root*TreeNode,iint)bool{ifroot==nil{returntrue}even:=i%2==0prev,ok:=d[i]if!ok{ifeven{prev=0}else{prev=10000000}}ifeven&&(root.Val%2==0||prev>=root.Val){returnfalse}if!even&&(root.Val%2==1||prev<=root.Val){returnfalse}d[i]=root.Valreturndfs(root.Left,i+1)&&dfs(root.Right,i+1)}returndfs(root,0)}