1609. Even Odd Tree

题目

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:

This image has an empty alt attribute; its file name is image-9.png

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

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/even-odd-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

class Solution {
    int[] tmp = new int[100000];
    public boolean isEvenOddTree(TreeNode root) {
        if(root == null) return false;        
        return traverseTree(root, 0);
    }

    public boolean traverseTree(TreeNode root, int level){
        if(root == null){
            return true;
        }else if((level%2 == 0 && (root.val %2 == 0 || (tmp[level] != 0 && root.val <= tmp[level])))  
            || (level%2 ==1 && (root.val%2 ==1 || (tmp[level] !=0 && root.val >= tmp[level])))){
            return false;
        }
        tmp[level] = root.val;
        boolean l = traverseTree(root.left, level+1);
        if(!l) return false;
        return traverseTree(root.right, level+1);
    }
}
执行结果:通过
执行用时:8 ms, 在所有 Java 提交中击败了98.70% 的用户
内存消耗:62.2 MB, 在所有 Java 提交中击败了20.43% 的用户

这里用了递归对树进行前序遍历,使用数组保存每层上次遍历到的节点值,内存消耗较多

官方题解

    public boolean isEvenOddTree(TreeNode root) {
        Queue<TreeNode> queue = new ArrayDeque<TreeNode>();
        queue.offer(root);
        int level = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            int prev = level % 2 == 0 ? Integer.MIN_VALUE : Integer.MAX_VALUE;
            for (int i = 0; i < size; i++) {
                TreeNode node = queue.poll();
                int value = node.val;
                if (level % 2 == value % 2) {
                    return false;
                }
                if ((level % 2 == 0 && value <= prev) || (level % 2 == 1 && value >= prev)) {
                    return false;
                }
                prev = value;
                if (node.left != null) {
                    queue.offer(node.left);
                }
                if (node.right != null) {
                    queue.offer(node.right);
                }
            }
            level++;
        }
        return true;
    }

使用双端数组队列和迭代方式。

执行用时:9 ms, 在所有 Java 提交中击败了96.96% 的用户
内存消耗:54.4 MB, 在所有 Java 提交中击败了80.00% 的用户

速度差不多,内存消耗少了很多。

Leave a Reply

Your email address will not be published. Required fields are marked *