LCR 176. 判断是否为平衡二叉树
题目
思路一:
使用一个函数进行判断,返回的是深度值,如果不是平衡二叉树则返回-1。
想合成一个函数,错误的:
class Solution {
public:
bool juge(TreeNode* root,int& h) //这里返回值是bool
{
if(root==NULL) return true;
if(root->left==NULL&&root->right==NULL)
{
h=1;
return true;
}
int lh=juge(root->left,h); //返回值是布尔类型,啊啊啊
int rh=juge(root->right,h);
if(lh>rh)
h=lh+1;
else h=rh+1;
if(abs(lh-rh)>1)
return false;
return juge(root->left,h)&&juge(root->right,h);
}
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
int h=1;
return juge(root,h);
}
};
错误的:改成一个递归后,判断条件漏掉了:左子树不平衡或右子树不平衡也要返回-1;
class Solution {
public:
int judge(TreeNode* root)
{
if(root==NULL) return 0;
if(root->left==NULL&&root->right==NULL) return 1;
int lh=judge(root->left);
int rh=judge(root->right);
if(abs(lh-rh)>1) return -1;
if(lh>rh) return lh+1;
return rh+1;
}
bool isBalanced(TreeNode* root) {
if(judge(root)!=-1)
return true;
return false;
}
};class Solution {
public:
int judge(TreeNode* root)
{
if(root==NULL) return 0;
if(root->left==NULL&&root->right==NULL) return 1;
int lh=judge(root->left);
int rh=judge(root->right);
if(abs(lh-rh)>1) return -1;
if(lh>rh) return lh+1;
return rh+1;
}
bool isBalanced(TreeNode* root) {
if(judge(root)!=-1)
return true;
return false;
}
};
看了答案后的:
class Solution {
public:
int judge(TreeNode* root)
{
if(root==NULL) return 0;
// if(root->left==NULL&&root->right==NULL) return 1;
int lh=judge(root->left);
if(lh==-1) return -1;
int rh=judge(root->right);
if(rh==-1) return -1;
if(abs(lh-rh)>1) return -1;
// if(lh>rh) return lh+1;
// return rh+1;
return max(lh,rh)+1;
}
bool isBalanced(TreeNode* root) {
if(judge(root)!=-1)
return true;
return false;
}
};
精简代码,直接:
return abs(rh-lh)>1?-1:max(lh,rh)+1;
return judge(root)!=-1
思路二:
先先序遍历,边计算深度边判断是否是平衡二叉树
ac的,分开来做了,两个递归,效率不高
class Solution {
public:
int calculateDepth(TreeNode* root) {
if(root==NULL) return 0;
if(root->left==NULL&&root->right==NULL) return 1;
int lh=calculateDepth(root->left);
int rh=calculateDepth(root->right);
if(lh>rh) return lh+1;
return rh+1;
}
bool isBalanced(TreeNode* root) {
if(root==NULL) return true;
int lh=calculateDepth(root->left);
int rh=calculateDepth(root->right);
if(abs(lh-rh)>1)
return false;
return isBalanced(root->left)&&isBalanced(root->right);
}
};
大佬写的精简化:
class Solution {
public boolean isBalanced(TreeNode root) {
if (root == null) return true;
return Math.abs(depth(root.left) - depth(root.right)) <= 1 && isBalanced(root.left) && isBalanced(root.right);
}
private int depth(TreeNode root) {
if (root == null) return 0;
return Math.max(depth(root.left), depth(root.right)) + 1;
}
}
作者:Krahets
链接:https://leetcode.cn/problems/ping-heng-er-cha-shu-lcof/solutions/159235/mian-shi-ti-55-ii-ping-heng-er-cha-shu-cong-di-zhi/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
时间复杂度:
O(NlogN)
空间复杂度
O(N)
LCR 146. 螺旋遍历二维数组
class Solution {
public:
vector<int> spiralArray(vector<vector<int>>& array) {
int l=0,r=array[0].size()-1,t=0,b=array.size()-1;
vector<int> res;
int x=0;
while(true)
{
for(int i=l;i<=r;i++) res[x++]=array[l][i]; //从左到右
if(++t>b) break;
for(int i=t;i<=b;i++) res[x++]=array[i][r]; //从上到下
if(--r>t) break;
for(int i=r;i>=l;i--) res[x++]=array[b][i]; //从右往左
if(--b>r) break;
for(int i=b;i>=t;i--) res[x++]=array[i][l]; //从下往上
if()
}
}
};
看的路飞的答案:啊啊不会啊!没咋看懂!
class Solution {
public:
vector<int> spiralArray(vector<vector<int>>& array) {
if(array.size()==0) return vector<int>(0);
int l=0,r=array[0].size()-1,t=0,b=array.size()-1;
vector<int> res((r+1)*(b+1));
int x=0;
while(true)
{
for(int i=l;i<=r;i++) res[x++]=array[t][i]; //从左到右
if(++t>b) break;
for(int i=t;i<=b;i++) res[x++]=array[i][r]; //从上到下
if(--r<l) break;
for(int i=r;i>=l;i--) res[x++]=array[b][i]; //从右往左
if(--b<t) break;
for(int i=b;i>=t;i--) res[x++]=array[i][l]; //从下往上
if(++l>r) break;
}
return res;
}
};