2 Star 1 Fork 0

royce li/Leetcode_royce

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
is_even_odd_tree.rs 1.28 KB
一键复制 编辑 原始数据 按行查看 历史
royce li 提交于 2021-12-28 17:08 . 2021/12/28
// Definition for a binary tree node.
#[derive(Debug, PartialEq, Eq)]
pub struct TreeNode {
pub val: i32,
pub left: Option<Rc<RefCell<TreeNode>>>,
pub right: Option<Rc<RefCell<TreeNode>>>,
}
impl TreeNode {
#[inline]
pub fn new(val: i32) -> Self {
TreeNode {
val,
left: None,
right: None
}
}
}
use std::rc::Rc;
use std::cell::RefCell;
struct Solution {}
impl Solution {
pub fn is_even_odd_tree(root: Option<Rc<RefCell<TreeNode>>>) -> bool {
let root = root.unwrap();
let mut stack = vec![root];
let mut layer = vec![];
let mut odd = false;
let mut now = 0;
while stack.len() != 0 {
while stack.len() != 0 {
let node = stack.remove(0);
let n = node.borrow();
// println!("{}", n.val);
if (now != 0 && (((now - n.val) > 0) ^ odd)) || ((n.val % 2 == 0) ^ odd) || now == n.val {
// println!("{} {} {}", now, n.val, odd);
return false;
}
now = n.val;
if let Some(l) = &n.left {
layer.push(l.clone());
}
if let Some(r) = &n.right {
layer.push(r.clone());
}
}
stack = layer;
layer = vec![];
odd = !odd;
now = 0;
}
true
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/royce-li/leetcode_royce.git
git@gitee.com:royce-li/leetcode_royce.git
royce-li
leetcode_royce
Leetcode_royce
master

搜索帮助