剑指offer55题目二

#include<iostream>
#include<algorithm>
using namespace std;

struct TreeNode {
	TreeNode* l;
	TreeNode* r;
	int value;
};

int b = 1;
int depth(TreeNode *t) {
	if (!t) {
		return 0;
	}
	if (depth(t->l) - depth(t->r) > 1 || depth(t->l) - depth(t->r) < -1) b = 0;
}

int main() {
	TreeNode *t = new TreeNode();
	t->value = 1;
	t->l = new TreeNode(); t->l->value = 2;
	t->r = new TreeNode(); t->r->value = 3;
	t->r->r = new TreeNode(); t->r->r->value = 4;
	t->l->l = new TreeNode(); t->l->l->value = 5;
	t->l->r = new TreeNode(); t->l->r->value = 7;
	t->l->r->l = new TreeNode(); t->l->r->l->value = 6;
	depth(t);
	if (b) {
		cout<< "是" <<endl;
	}
	else {
		cout<< "否" <<endl;
	}
	return 0;
}

阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页