C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool isEvenOddTree(TreeNode* root) {
}
};
运行代码
提交
java 解法, 执行用时: 9 ms, 内存消耗: 57.7 MB, 提交时间: 2022-12-04 12:49:46
/**
* 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 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;
}
}
golang 解法, 执行用时: 156 ms, 内存消耗: 18.6 MB, 提交时间: 2022-12-04 12:49:30
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isEvenOddTree(root *TreeNode) bool {
q := []*TreeNode{root}
for level := 0; len(q) > 0; level++ {
prev := 0
if level%2 == 1 {
prev = math.MaxInt32
}
size := len(q)
for _, node := range q {
val := node.Val
if val%2 == level%2 || level%2 == 0 && val <= prev || level%2 == 1 && val >= prev {
return false
}
prev = val
if node.Left != nil {
q = append(q, node.Left)
}
if node.Right != nil {
q = append(q, node.Right)
}
}
q = q[size:]
}
return true
}
javascript 解法, 执行用时: 216 ms, 内存消耗: 73 MB, 提交时间: 2022-12-04 12:49:17
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isEvenOddTree = function(root) {
const queue = [];
queue.push(root);
let level = 0;
while (queue.length) {
const size = queue.length;
let prev = level % 2 == 0 ? -Number.MAX_VALUE : Number.MAX_VALUE;
for (let i = 0; i < size; i++) {
const node = queue.shift();
const 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) {
queue.push(node.left);
}
if (node.right) {
queue.push(node.right);
}
}
level++;
}
return true;
};
python3 解法, 执行用时: 400 ms, 内存消耗: 42 MB, 提交时间: 2022-12-04 12:49:03
# 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 = right
class Solution:
def isEvenOddTree(self, root: Optional[TreeNode]) -> bool:
queue = [root]
level = 0
while queue:
prev = float('inf') if level % 2 else 0
nxt = []
for node in queue:
val = node.val
if val % 2 == level % 2 or level % 2 == 0 and val <= prev or level % 2 == 1 and val >= prev:
return False
prev = val
if node.left:
nxt.append(node.left)
if node.right:
nxt.append(node.right)
queue = nxt
level += 1
return True