leetcode 奇偶数

#include<iostream>
#include<queue>

using namespace std;
struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(){}
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
	TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
void pretravel(TreeNode* root){
	if (root == NULL){
		return;
	}
	else{
		cout << root->val << " ";
		pretravel(root->left);
		pretravel(root->right); 
	}
}
//建立二叉树通过一数组 
TreeNode* buildCore(int *piValue, int Start, int Length){
	if (Start>= Length){
		return NULL;
	}
	TreeNode *pRoot = new TreeNode;
	if (piValue[Start] == 0) pRoot = NULL;
	else{
		pRoot->val = piValue[Start];
		pRoot->left  = buildCore(piValue, Start*2+1, Length);
		pRoot->right = buildCore(piValue, Start*2+2, Length);
	}
	return pRoot;
}


int isEvenOddTree(TreeNode *root){
	queue<TreeNode*>Q;
	Q.push(root);
	int level = 0;
	while(!Q.empty()){
		int pre = level % 2 == 0 ? INT_MIN :INT_MAX;
		int len = Q.size() ;
		for(int i = 0;i < len;i++){
			TreeNode* node = Q.front();
			Q.pop();
			int value = node->val;
			if(value % 2 == level % 2) return false;
			if((level % 2 == 0 && pre >= value) ||(level % 2 == 1 && pre <= value)) return false;
			pre = value;
			if(node->left){
				Q.push(node->left);
			}
			if(node->right){
				Q.push(node->right);
			}
		}
		level++;
	}
	return true;
}
int main(){
	TreeNode* Tree;
	int root1[15] = {1,10,4,3,0,7,9,12,8,0,0,6,0,0,2};
	int* a = root1;
	Tree = buildCore(a,0,15);
	cout << isEvenOddTree(Tree) << endl;
    return 0;

} 

题目来源

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值