今天是二叉树的第三天学习,学到的知识点有:如何判断平衡二叉树,使用回溯法求解二叉树的所有可行路径,求解左叶子之和,通过满二叉树的判断高效求解完全二叉树的结点个数。
110.平衡二叉树
题目链接:110. 平衡二叉树 - 力扣(LeetCode)
判断二叉树是否是平衡二叉树,主要依据是判断左子树和右子树的高度差是否大于1。首先对于遍历方式,需要得到左子树和右子树的高度后才能进行求解。所以应采用后序遍历。这里还有一个比较巧妙的地方,当存在左右子树高度差大于一的情况时,就直接返回-1.这个-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 order(TreeNode* root)
{
if(root==NULL) return 0;
int leftheight=order(root->left);
if(leftheight==-1) return -1;
int rightheight=order(root->right);
if(rightheight==-1) return -1;
if(abs(leftheight-rightheight)>1) return -1;
else return max(leftheight,rightheight)+1;
}
bool isBalanced(TreeNode* root) {
int result=order(root);
if(result==-1) return false;
else return true;
}
};
257. 二叉树的所有路径
题目链接:257. 二叉树的所有路径 - 力扣(LeetCode)
这个题目涉及到回溯的思想。当遇到空结点时,就将尾部的节点弹出,从而实现回溯的效果。(其实理解的有点一知半解,有点懵)
/**
* 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 order(TreeNode* root,vector<int> &vec,vector<string> &result)
{
vec.push_back(root->val);
if(root->left==NULL&&root->right==NULL)
{
string s;
for(int i=0;i<vec.size()-1;i++)
{
s+=to_string(vec[i]);
s+="->";
}
s+=to_string(vec[vec.size()-1]);
result.push_back(s);
}
if(root->left!=NULL)
{
order(root->left,vec,result);
vec.pop_back();
}
if(root->right!=NULL)
{
order(root->right,vec,result);
vec.pop_back();
}
}
vector<string> binaryTreePaths(TreeNode* root) {
vector<string> result;
vector<int> vec;
order(root,vec,result);
return result;
}
};
404.左叶子之和
题目链接:404. 左叶子之和 - 力扣(LeetCode)
刚拿到这个题,第一想法是使用中序遍历的迭代法,其本质思想是先一直往左走,遇到空结点再回溯往右走。很适合这个题求解左叶子的要求。
/**
* 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) {
stack<TreeNode*> st;
int result=0;
TreeNode* cur=root;
while(cur!=NULL||!st.empty())
{
if(cur!=NULL)
{
st.push(cur);
if(cur->left!=NULL&&cur->left->left==NULL&&cur->left->right==NULL) result+=cur->left->val;
cur=cur->left;
}
else
{
cur=st.top();
st.pop();
cur=cur->right;
}
}
return result;
}
};
后来看了随想录的解法,使用了递归的思路。使用的同样是中序遍历,先判断左子树中左叶子节点的和,然后判断该节点下是否有左叶子结点,再求右子树中左叶子结点的和。并求和作为该节点下左叶子结点的和。具体代码实现如下:
/**
* 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 order(TreeNode* root)
{
if(root==NULL) return 0;
if(root->left==NULL&&root->right==NULL) return 0;
int left=order(root->left);
if(root->left!=NULL&&root->left->left==NULL&&root->left->right==NULL) left=root->left->val;
int right=order(root->right);
int result=left+right;
return result;
}
int sumOfLeftLeaves(TreeNode* root) {
int result=order(root);
return result;
}
};
222.完全二叉树的节点个数
题目链接:222. 完全二叉树的节点个数 - 力扣(LeetCode)
这个题目我的第一想法是直接在遍历过程中对结点进行计数,算法复杂度为log(N)。
/**
* 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 order(TreeNode* root)
{
if(root==NULL) return 0;
if(root->left==NULL&&root->right==NULL) return 1;
int left=order(root->left);
int right=order(root->right);
int result=left+right+1;
return result;
}
int countNodes(TreeNode* root) {
int result=order(root);
return result;
}
};
但由于这是完全二叉树,有更高效的求解方法。完全二叉树中一定存在一些满二叉树,如果一棵树左子树深度和右子树深度相同,就是满二叉树。可以直接使用得到 pow(2,rightdepth)-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 countNodes(TreeNode* root) {
if(root==NULL) return 0;
int leftdepth=1;
int rightdepth=1;
TreeNode* leftnode=root->left;
TreeNode* rightnode=root->right;
while(leftnode!=NULL)
{
leftnode=leftnode->left;
leftdepth++;
}
while(rightnode!=NULL)
{
rightnode=rightnode->right;
rightdepth++;
}
if(rightdepth==leftdepth) return pow(2,rightdepth)-1;
return countNodes(root->left)+countNodes(root->right)+1;
}
};