BM32 合并二叉树
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param t1 TreeNode类
* @param t2 TreeNode类
* @return TreeNode类
*/
TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2) {
// write code here
if(!t1){
return t2;
}
if(!t2){
return t1;
}
TreeNode* ret = new TreeNode(t1->val+t2->val);
ret->left = mergeTrees(t1->left,t2->left);
ret->right=mergeTrees(t1->right,t2->right);
return ret;
}
};
BM33 二叉树的镜像
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return TreeNode类
*/
TreeNode* Mirror(TreeNode* pRoot) {
// write code here
if(!pRoot)
return NULL;
else{
swap<TreeNode*>(pRoot->left,pRoot->right);
Mirror(pRoot->left);
Mirror(pRoot->right);
}
return pRoot;
}
};
--------------------------------------------分割线,下面是不用swap解法
TreeNode* Mirror(TreeNode* pRoot) {
// write code here
if(pRoot == NULL){
return NULL;
}
TreeNode* left = Mirror(pRoot->left);
TreeNode* right = Mirror(pRoot->right);
pRoot->left = right;
pRoot->right = left;
return pRoot;
}
BM34 判断是不是二叉搜索树
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
bool isValidBST(TreeNode* root) {
// write code here
return dfs(root,INT_MIN,INT_MAX);
}
bool dfs(TreeNode* root,int left,int right){
if(root==nullptr)
return true;
if(root->val<left||root->val>right)
return false;
return dfs(root->left,left,root->val)&&dfs(root->right,root->val,right);
}
};
BM35 判断是不是完全二叉树
如果没有左子节点,有右子节点,则不满足完全二叉树;
如果有左子节点,则入队;
如果有右子节点,则入队,否则支flag为1;
如果flag==1并且有子节点,则不满足完全二叉树;
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @return bool布尔型
*/
bool isCompleteTree(TreeNode* root) {
// write code here
if(root==nullptr)
return true;
queue<TreeNode*> q;
q.push(root);
int flag=0;
while(!q.empty()){
TreeNode* t=q.front();
q.pop();
if(flag==1&&(t->left||t->right))
return false;
if(t->left==nullptr&&t->right)
return false;
if(t->left)
q.push(t->left);
if(t->right)
q.push(t->right);
else
flag=1;
}
return true;
}
};
BM36 判断是不是平衡二叉树
class Solution {
public:
bool IsBalanced_Solution(TreeNode* pRoot) {
if(pRoot==NULL)
return true;
int lefth = hight(pRoot->left);
int righth=hight(pRoot->right);
if(abs(lefth-righth)>1)
return false;
return IsBalanced_Solution(pRoot->left)&&IsBalanced_Solution(pRoot->right);
}
int hight(TreeNode* pRoot){
if(!pRoot)
return 0;
if(!pRoot->left && !pRoot->right)
return 1;
return 1+ max(hight(pRoot->left), hight(pRoot->right));
}
};
BM37 二叉搜索树的最近公共祖先
由于是搜索树,存在以下三种情况:
1、子节点都在父节点的左边,值小于此节点;
2、子节点都在父节点的右边,值大于此节点;
3、值等于父节点,即直接跳出可以返回此节点;
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
class Solution {
private:
int ret=0;
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param root TreeNode类
* @param p int整型
* @param q int整型
* @return int整型
*/
int lowestCommonAncestor(TreeNode* root, int p, int q) {
// write code here
TreeNode* cur=root;
while(1){
if(p<cur->val&&q<cur->val)
cur=cur->left;
else if(p>cur->val&&q>cur->val)
cur=cur->right;
else break;
}
return cur->val;
}
};
BM38 在二叉树中找到两个节点的最近公共祖先*
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
class Solution {
public:
/**
*
* @param root TreeNode类
* @param o1 int整型
* @param o2 int整型
* @return int整型
*/
int lowestCommonAncestor(TreeNode* root, int o1, int o2) {
// write code here
TreeNode* l = fun(root,o1,o2);
return l->val;
}
TreeNode* fun(TreeNode* root,int o1,int o2){
if(!root) return NULL;
if(root->val==o1||root->val==o2) return root;
TreeNode* ln = fun(root->left,o1,o2);
TreeNode* rn = fun(root->right,o1,o2);
if(!ln) return rn;
if(!rn) return ln;
return root;
}
};
BM40 重建二叉树
后序与中序,确定二叉树
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
TreeNode* traversal(vector<int>& inorder,vector<int>& postorder){
//如果后序数组为空,则返回空
if(postorder.size()==0) return NULL;
//分离出后序数组的根节点,用rootvalue存起来
int rootValue = postorder[postorder.size()-1];
//创建一个新节点,用来存储新的树
TreeNode* root = new TreeNode(rootValue);
//当后序数组只有这一个节点时,返回根节点;
if(postorder.size()==1) return root;
//遍历中序数组,找到其中的根节点
int delimiterIndex;
for(delimiterIndex=0;delimiterIndex<inorder.size();delimiterIndex++){
if(inorder[delimiterIndex]==rootValue) break;
}
//切割中序数组
//左闭右开区间:【0,delimiterIndex】
vector<int> leftInorder(inorder.begin(),inorder.begin()+delimiterIndex);
//【delimiterIndex+1,end】
vector<int> rightInorder(inorder.begin()+delimiterIndex+1,inorder.end());
//分割玩后,将后序数组中的根节点丢弃
postorder.resize(postorder.size()-1);
//切割后序数组
//亦然左闭右开,注意这里使用了左中序数组大小作为切割点,【0,leftInorder.size】
vector<int> leftPostorder(postorder.begin(),postorder.begin()+leftInorder.size());
//【leftInorder.size(),end】
vector<int> rightPostorder(postorder.begin()+leftInorder.size(),postorder.end());
//创建左边的子树,对分离的前后序数组进行再分离
root->left=traversal(leftInorder,leftPostorder);
root->right=traversal(rightInorder, rightPostorder);
return root;
}
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
if(pre.size()==0||vin.size()==0) return NULL;
return traversal(pre,vin);
}
};
前序与中序
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
TreeNode* traversal (vector<int>& inorder, int inorderBegin, int inorderEnd, vector<int>& preorder, int preorderBegin, int preorderEnd) {
if (preorderBegin == preorderEnd) return NULL;
int rootValue = preorder[preorderBegin]; // 注意用preorderBegin 不要用0
TreeNode* root = new TreeNode(rootValue);
if (preorderEnd - preorderBegin == 1) return root;
int delimiterIndex;
for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {
if (inorder[delimiterIndex] == rootValue) break;
}
// 切割中序数组
// 中序左区间,左闭右开[leftInorderBegin, leftInorderEnd)
int leftInorderBegin = inorderBegin;
int leftInorderEnd = delimiterIndex;
// 中序右区间,左闭右开[rightInorderBegin, rightInorderEnd)
int rightInorderBegin = delimiterIndex + 1;
int rightInorderEnd = inorderEnd;
// 切割前序数组
// 前序左区间,左闭右开[leftPreorderBegin, leftPreorderEnd)
int leftPreorderBegin = preorderBegin + 1;
int leftPreorderEnd = preorderBegin + 1 + delimiterIndex - inorderBegin; // 终止位置是起始位置加上中序左区间的大小size
// 前序右区间, 左闭右开[rightPreorderBegin, rightPreorderEnd)
int rightPreorderBegin = preorderBegin + 1 + (delimiterIndex - inorderBegin);
int rightPreorderEnd = preorderEnd;
root->left = traversal(inorder, leftInorderBegin, leftInorderEnd, preorder, leftPreorderBegin, leftPreorderEnd);
root->right = traversal(inorder, rightInorderBegin, rightInorderEnd, preorder, rightPreorderBegin, rightPreorderEnd);
return root;
}
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
if (vin.size() == 0 || pre.size() == 0) return NULL;
// 参数坚持左闭右开的原则
return traversal(vin, 0, vin.size(), pre, 0, pre.size());
}
};
BM41 输出二叉树的右视图
先确定树的结构,再层序遍历确定每层最后出现的节点。
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
* 求二叉树的右视图
* @param xianxu int整型vector 先序遍历
* @param zhongxu int整型vector 中序遍历
* @return int整型vector
*/
// 构造树的每个结点
struct node{
node* left;
node* right;
int val;
};
// 先获取整棵树的结构
node* create(int pL,int pR,int mL,int mR,vector<int> &xianxu, vector<int>& zhongxu){
if(pL>pR){
return NULL;
}
node* root=new node;
root->val=xianxu[pL];
int k;
for(k=mL;k<=mR;k++){
if(zhongxu[k]==xianxu[pL]){
break;
}
}
int numLeft=k-mL;
// 递归左子树
root->left=create(pL+1,pL+numLeft,mL,k-1,xianxu, zhongxu);
// 递归右子树
root->right=create(pL+numLeft+1,pR,k+1,mR,xianxu,zhongxu);
return root;
}
unordered_map<int, int> mp;
// 我们按照层序遍历这颗二叉树,然后记录每个层数最后出现的结点的权值
void BFS(node* root){
if(!root){
return;
}
queue<pair<node*,int> > q;
q.push(make_pair(root,1));
mp[1]=root->val;
while(!q.empty()){
auto now=q.front();
q.pop();
// 先左子树,然后遍历右子树
if(now.first->left){
q.push(make_pair(now.first->left,now.second+1));
mp[now.second+1]=now.first->left->val;
}
if(now.first->right){
q.push(make_pair(now.first->right,now.second+1));
mp[now.second+1]=now.first->right->val;
}
}
}
vector<int> solve(vector<int>& xianxu, vector<int>& zhongxu) {
// write code here
node* root=create(0,xianxu.size()-1,0,zhongxu.size(),xianxu,zhongxu);
vector<int> ans;
BFS(root);
int n=mp.size(); // 层数
for(int i=1;i<=n;i++){
ans.push_back(mp[i]);
}
return ans;
}
};
- 本文链接:https://archer-lan.github.io/2022/03/22/%E7%89%9B%E5%AE%A2%E5%88%B7%E9%A2%98-DAY6/
- 版权声明:本博客所有文章除特别声明外,均默认采用 许可协议。