Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/copilot/src/extension/prompts/node/test/fixtures/problem1.cpp
13406 views
1
#include "BinaryTreeProblems.h"
2
#include<stack>
3
/*
4
* 题目1: 统计二叉树中度为1的结点个数
5
*
6
* 请在此处完成你的代码:
7
*/
8
int countDegreeOneNodes(TreeNode* rootss) {
9
int ans=0;
10
auto root=rootss;
11
std::stack<TreeNode*>s;
12
while (!s.empty() or root)
13
{
14
while(root){
15
s.push(root);
16
root=root->left;
17
}
18
if (!s.empty())
19
{
20
auto top=s.top();
21
s.pop();
22
if (top->left==nullptr and top->right==nullptr)
23
{
24
if(top!=rootss) ans+=1;
25
}
26
root=root->right;
27
}
28
}
29
if(rootss->left==nullptr xor rootss->right==nullptr) ans+=1;
30
// 在这里实现函数
31
return ans;
32
}
33
34