二叉树的最大深度
recursive method:
AC代码:
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==NULL) return 0;
int lDepth = maxDepth(root->left);
int rDepth = maxDepth(root->right);
return 1+(lDepth>rDepth?lDepth:rDepth);
}
};
验证二叉搜索树
AC代码:
class Solution {
public:
bool isValidBST(TreeNode* root) {
//左子树右子树都是BST,且左的最大小于root,右的最小大于root
if(root==NULL) return true; //空树是BTS
if(!isValidBST(root->left)||!isValidBST(root->right))
return false;
TreeNode* lmax=max(root->left),*rmin=min(root->right);
if(!(lmax==NULL||lmax->val<root->val))
return false;
if(!(rmin==NULL||rmin->val>root->val))
return false;
return true;
}
//找以root为根的树的最小值所在节点的指针
TreeNode* min(TreeNode* root)
{
if(root==NULL)
return NULL;
if(root->left==NULL)
return root;
return min(root->left);
}
TreeNode* max(TreeNode* root)
{
if(root==NULL)
return NULL;
if(root->right==NULL)
return root;
return max(root->right);
}
};
对称二叉树
错误代码:
class Solution {
public:
bool isSymmetric(TreeNode* root) {
//左右两个子树各自对称,且这两个是镜像的
if(root==NULL) return true;
if(!(isSymmetric(root->left)&&isSymmetric(root->right)))
return false; //如果一个不对称,就整体不对称
queue<int> vals;
TreeNode* p1 = root->left,*p2=root->right;
leftVis(p1,vals);
return rightVis(p2,vals);
}
void leftVis(TreeNode* root,queue<int>& q)
{
if(root==NULL) return;
else
{
leftVis(root->left,q);
q.push(root->val);
leftVis(root->right,q);
}
}
bool rightVis(TreeNode* root,queue<int>& q)
{
if(root!=NULL)
{
if(!rightVis(root->right,q)) return false;
if(q.empty()) return false;
int cur = q.front();
q.pop();
if(root->val!=cur)
return false;
if(!rightVis(root->left,q)) return false;
}
if(q.empty()) return true;
else return false;
}
};
二叉树的层次遍历
思路:
层次遍历需要queue,不同的深度放在不同的子vector中,因此也需要记录该节点的深度,所以可以使用pair队来保存信息。
注意当给出的树空时这种corner case。
当某个深度第一次遇到时,先要创建这个vector。
AC代码:
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
//将pair对入队
vector<vector<int>> ans;
if(root==NULL) return ans;
queue<pair<TreeNode*,int>> q;
q.push(make_pair(root,0));
int depth = -1;
while(!q.empty())
{
pair<TreeNode*,int> p = q.front();
int curDepth = p.second;
TreeNode* curNode = p.first;
q.pop();
//if:这个高度第一次出现,创建子链表
if(curDepth!=depth)
{
depth = curDepth;
vector<int> newDepth;
newDepth.push_back(curNode->val);
ans.push_back(newDepth);
}
else //直接往子链表insert
{
ans[depth].push_back(curNode->val);
}
if(curNode->left!=NULL) q.push(make_pair(curNode->left,curDepth+1));
if(curNode->right!=NULL) q.push(make_pair(curNode->right,curDepth+1));
}
return ans;
}
};
将有序数组转换为二叉搜索树
错误代码(malloc-demalloc错误)
class Solution {
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
//如果nums中没有数字,则返回空。有一个,则直接创建返回。
//有两个或两个以上:如果是奇数个,则中间的作为根,偶数个随便
int len = nums.size();
if(len==0)
return NULL;
else if(len==1)
{ TreeNode* p = (TreeNode*)malloc(sizeof(TreeNode));
p->val = nums[0];
p->left =p->right= NULL;
return p;
}
else
{
vector<int> leftN,rightN;
int bound = len%2;
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->val = nums[bound];
for(int i = 0;i<bound;i++) leftN.push_back(nums[i]);
for(int i = bound+1;i<len;i++) rightN.push_back(nums[i]);
root->left = sortedArrayToBST(leftN);
root->right = sortedArrayToBST(rightN);
return root;
}
}
}
;