Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create 1609. Even Odd Tree#123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
divyalakhwani31 wants to merge1 commit intofishercoder1534:master
base:master
Choose a base branch
Loading
fromdivyalakhwani31:patch-1
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletionscpp/1609. Even Odd Tree
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
/**
* 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) {
vector<vector<int>> v;
queue<pair<TreeNode*, int>> q;
q.push({root, 0});

while(!q.empty()) {
int sz = q.size();
vector<int> tmp;
for(int i = 0; i < sz; ++i) {
pair<TreeNode*, int> p = q.front();
q.pop();
int val = p.first->val, l = p.second;
tmp.push_back(val);

if(p.first->left) q.push({p.first->left, l+1});
if(p.first->right) q.push({p.first->right, l+1});
}

v.push_back(tmp);
}

for(int i = 0; i < v.size(); ++i) {
if(i%2 == 0) {
if(v[i].size() > 0) {
vector<int> tmp = v[i];
sort(tmp.begin(), tmp.end());
if(v[i] != tmp) return false;
for(int i = 0; i < tmp.size(); ++i) {
if(i > 0 and tmp[i-1] == tmp[i]) return false;
if(tmp[i]%2 == 0) return false;
}
}
} else {
if(v[i].size() > 0) {
vector<int> tmp = v[i];
sort(tmp.rbegin(), tmp.rend());
if(v[i] != tmp) return false;
for(int i = 0; i < tmp.size(); ++i) {
if(i > 0 and tmp[i-1] == tmp[i]) return false;
if(tmp[i]%2 != 0) return false;
}
}
}
}

return true;
}
};

[8]ページ先頭

©2009-2025 Movatter.jp