方法一采用广度优先遍历-层序遍历
/**
* 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:
int countNodes(TreeNode* root) {
int sum=0;
if(root==nullptr) return 0;
queue<TreeNode*> que;
que.push(root);
while(!que.empty())
{
int size=que.size();
sum=sum+size;
for(int i=0;i<size;i++)
{
TreeNode* node=que.front();
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return sum;
}
};
方法二:普通二叉树------递归遍历
/**
* 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:
int countNodes(TreeNode* root) {
if(root==nullptr) return 0;
int left=countNodes(root->left);
int right=countNodes(root->right);
int res=left+right+1;
return res;
}
};
方法三完全二叉树-递归
/**
* 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:
int countNodes(TreeNode* root) {
if(root==nullptr) return 0;
TreeNode* left=root->left;
TreeNode* right=root->right;
int lh=0,rh=0;
while(left)
{
left=left->left;
lh++;
}
while(right)
{
right=right->right;
rh++;
}
if(lh==rh)
{
return (2<<lh)-1;
}//递归终止判断条件
return countNodes(root->left)+countNodes(root->right)+1;
}
};
/**
* 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:
int function(TreeNode* root)
{
if(root==nullptr) return 0;
int lefth=function(root->left);
int righth=function(root->right);
int res=max(lefth,righth)+1;
return res;
}
bool isBalanced(TreeNode* root) {
if(root==nullptr) return true;
int h1=function(root->left);
int h2=function(root->right);
if(abs(h1-h2)>1)
{
return false;
}//递归终止条件
bool a=isBalanced(root->left);
bool b=isBalanced(root->right);
return a&&b;
}
};
先写出判断左右子树高度的函数,在判断平衡二叉树里面写出终止判断条件,当左右子树高度相差一的时候返回,再进行递归遍历
/**
* 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:
int function(TreeNode* root)
{
if(root==nullptr) return 0;//递归终止逻辑
//单层递归逻辑
int lefth=function(root->left);
if(lefth==-1) return -1;//如果子树已经不平衡的话,就直接返回
int righth=function(root->right);
if(righth==-1) return -1;
int res=0;
if(abs(lefth-righth)>1)
{
res=-1;
}
else
{
res=max(lefth,righth)+1;
}
return res;
}
bool isBalanced(TreeNode* root) {
if(root==nullptr) return true;
int h=function(root);
if(h==-1)
{
return false;
}
return true;
}
};
因为你可能在想:
"当我走到一个节点,发现它是空的,说明上一个节点是最后一个有效节点,所以那条路径结束了。"
这个想法有道理,但不正确的原因:
-
时机不对 :在空节点处,我们已经离开了真正的叶子节点
-
重复问题:一个叶子节点有两个空孩子,会被记录两次
-
语义不清:空节点不是树的组成部分
所以我们进行判断时必须是判断叶子节点,不能使用判断空节点
/**
* 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:
void traverse(TreeNode *cur,vector<int> &path,vector<string> & res)
{
path.push_back(cur->val);
if(cur->left==nullptr&&cur->right==nullptr)//判断是否为路径终点,如果为终点进行转换
{
string spath;
for(int i=0;i<path.size()-1;i++)
{
spath+=to_string(path[i]);
spath+="->";
}
spath+=to_string(path[path.size()-1]);
res.push_back(spath);
return;
}
if(cur->left)
{
traverse(cur->left,path,res);
path.pop_back();
}
if(cur->right)
{
traverse(cur->right,path,res);
path.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<int> path;
vector<string> res;
traverse(root,path,res);
return res;
}
};
vector容器后插和删除要使用push_back和pop_back
迭代法-层序遍历-广度优先遍历
/**
* 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:
int sumOfLeftLeaves(TreeNode* root) {
if(root==nullptr) return 0;
queue<TreeNode*> que;
que.push(root);
int res=0;
while(!que.empty())
{
int size=que.size();
for(int i=0;i<size;i++)
{
TreeNode* node=que.front();
que.pop();
if(node->left!=nullptr) que.push(node->left);
if(node->right!=nullptr) que.push(node->right);
if(node->left!=nullptr&&node->left->left==nullptr&&node->left->right==nullptr)
{
res=res+node->left->val;
}
}
}
return res;
}
};
递归法
/**
* 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:
int sumOfLeftLeaves(TreeNode* root) {
if(root==nullptr) return 0;
if(root->left==nullptr&&root->right==nullptr) return 0;
int leftnum=sumOfLeftLeaves(root->left);
if(root->left!=nullptr&&root->left->left==nullptr&&root->left->right==nullptr)
{
leftnum=root->left->val;
}
int rightnum=sumOfLeftLeaves(root->right);
int sum=leftnum+rightnum;
return sum;
}
};