剑指offer-树相关
树相关
1.重建二叉树
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {
if(pre.size() == || vin.size() == )
return nullptr;
return constructCore(pre, vin, , pre.size()-, , vin.size()-);
} TreeNode* constructCore(vector<int> pre,vector<int> vin, int sp, int ep, int si, int ei)
{
//sp, ep 确定先序中树的范围,si,ei确定中序的子树范围
//先序头就是根
int rootValue = pre[sp];
TreeNode* root = new TreeNode(rootValue);
if(sp == ep)
{
if(si == ei && pre[sp] == vin[si])
return root;
}
//中序找到根,分为左右子树
int index = si;
while(index <= ei && vin[index] != rootValue)
++index;
int leftLen = index-si;//左子树长度
if(leftLen > )
{
root->left = constructCore(pre, vin, sp+, sp+leftLen, si, index-);
}
if(leftLen < ep-sp)
{
root->right = constructCore(pre, vin, sp+leftLen+, ep, index+, ei);
}
return root;
}
};
2.树的子结构
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
//tree2空或者tree1空都不符合,问清楚空树是不是子树
if(pRoot2 == nullptr || pRoot1 == nullptr)
return false;
bool res = false;
if(pRoot1 != nullptr && pRoot2 != nullptr)
{
if(pRoot1->val == pRoot2->val)//找到匹配的根
res = isSubTree(pRoot1, pRoot2);
if(!res)//未找到,递归左节点
res = HasSubtree(pRoot1->left, pRoot2);
if(!res)//未找到,递归节点
res = HasSubtree(pRoot1->right, pRoot2);
}
return res;
}
//判断是否是子树
bool isSubTree(TreeNode* pRoot1, TreeNode* pRoot2)
{
//tree2先遍历结束,重合
if(pRoot2 == nullptr)
return true;
//主树先结束,不重合
if(pRoot1 == nullptr)
return false;
//对于节点值不等,不重合
if(pRoot1->val != pRoot2->val)
return false; //节点值相等,递归判断左右子树
return isSubTree(pRoot1->left, pRoot2->left)
&& isSubTree(pRoot1->right, pRoot2->right);
}
};
3.二叉树镜像
class Solution {
public:
void Mirror(TreeNode *pRoot) {
//前序遍历(根左右)树每个节点,如果节点有子节点,就交换子节点,直到叶子节点
//处理空树
if(pRoot == nullptr)
return;
//到达叶子节点
if(pRoot->left == nullptr && pRoot->right == nullptr)
return;
//交换子节点
TreeNode* tmp = pRoot->left;
pRoot->left = pRoot->right;
pRoot->right = tmp;
//递归左子树
if(pRoot->left)
Mirror(pRoot->left);//不要return,因为void不返回 //递归右子树
if(pRoot->right)
Mirror(pRoot->right);
}
};
4.对称二叉树
class Solution {
public:
bool isSymmetrical(TreeNode* pRoot)
{
return isSymmetrical(pRoot, pRoot);
} bool isSymmetrical(TreeNode* pRoot1, TreeNode* pRoot2)
{
if(pRoot1 == nullptr && pRoot2 == nullptr)//都空,对称
return true;
if(pRoot1 == nullptr || pRoot2 == nullptr)//一空一不空。不对称
return false;
if(pRoot1->val != pRoot2->val)//都不空,值不等,不对称,写了return true就无法再往下遍历了
return false;
//都不空,且值相等,在比较下一个节点
return isSymmetrical(pRoot1->left, pRoot2->right)
&& isSymmetrical(pRoot1->right, pRoot2->left);
}
};
5.二叉树前中后续遍历,递归和循环版
递归就是递归,循环实现用栈(前序中序比较简单,后续的话需要简单修改一下前序遍历,反转即可)
前序遍历非递归版
class Solution {
public:
vector<int> res;
vector<int> preorderTraversal(TreeNode *root) {
if(!root)
return {};
stack<TreeNode*> s;
s.push(root);
while(!s.empty())
{
auto p = s.top();
s.pop();
res.push_back(p->val);
if(p->right)
s.push(p->right);
if(p->left)
s.push(p->left);
}
return res;
}
};
6.后序遍历非递归版
class Solution {
public:
//一个巧妙地算法,先序遍历:根->左->右,根据出栈顺序根->右->左reverse,变成 左->右->根
vector<int> res;
vector<int> postorderTraversal(TreeNode *root) {
if(root == nullptr)
return {};
stack<TreeNode*> s;
s.push(root);
while(!s.empty())
{
auto p = s.top();
s.pop();
res.push_back(p->val);
if(p->left)
s.push(p->left);
if(p->right)
s.push(p->right);
}
reverse(res.begin(), res.end());
return res;
}
};
7.二叉树中和为某值的路径
class Solution {
public:
vector<vector<int> > res;
//递归解决,防止段错误
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
if(root == nullptr)
return res;
vector<int> cur;
find(root, expectNumber, cur);
return res;
} void find(TreeNode* root, int target, vector<int> cur)
{
if(root == nullptr)
return ;
cur.push_back(root->val);
if((target-root->val) == && root->left == nullptr && root->right == nullptr)
res.push_back(cur);
else
{
if(root->left)
find(root->left, target-root->val, cur);
if(root->right)
find(root->right, target-root->val, cur);
}
}
};
8.二叉树的深度
(递归)
class Solution {
public:
int res = ;
int TreeDepth(TreeNode* pRoot)
{
int depth = ;
getDepth(pRoot, depth);
return res;
} //递归函数,每遇到null节点说明到底了,更新res
void getDepth(TreeNode* root, int depth)
{
if(root == nullptr)
{
res = (depth > res) ? depth : res;
return ;
}
else
{
getDepth(root->left, depth+);
getDepth(root->right, depth+);
}
}
};
非递归(使用队列BFS层序遍历 数层数)
class Solution {
public:
//非递归,使用BFS栈来解决问题,其中的打印内容可以辅助查看具体过程
int TreeDepth(TreeNode *pRoot)
{
if (pRoot == nullptr)
return ;
queue<TreeNode *> s;
s.push(pRoot);
int depth = ;
while (!s.empty())
{
depth++;
int len = s.size();
cout << "第" << depth << "层,共有节点:" << len << "个" << endl;
for (int i = ; i < len; i++)
{
auto p = s.front();
s.pop();
cout << p->val << "出栈" << endl;
if (p->left)
{
s.push(p->left);
cout << p->left->val << "入栈" << endl;
} if (p->right)
{
s.push(p->right);
cout << p->right->val << "入栈" << endl;
}
}
}
return depth;
}
};
9.二叉树的最小深度(使用队列来实现,将上面一个算法简单改改即可:在第一次左右子节点都不存在时,跳出即可)
class Solution {
public:
//那我也用层序遍历(BFS)试试吧
int run1(TreeNode *root) {
if(root == nullptr)
return ; queue<TreeNode*> q;
q.push(root);
int depth = ;
while(!q.empty())
{
depth++;
int len = q.size();
for(int i=; i<len; i++)
{
auto p = q.front();
q.pop();
if(p->left == nullptr && p->right == nullptr)
return depth;
if(p->left)
q.push(p->left);
if(p->right)
q.push(p->right);
}
}
return depth;
}
//从上述方法我们可以想到,求算二叉树深度,也可以采用此法,跟我们原来的做法略有不同 //神奇的递归做法
int run(TreeNode *root) {
if(root == nullptr)
return ;
int l = run(root->left);
int r = run(root->right);
if(l == || r == )
return +l+r;
else
return +min(l, r);
}
};
序列化和反序列化二叉树
剑指offer-树相关的更多相关文章
- 剑指offer——树的子结构 (JAVA代码)
版权声明:本文为博主原创文章,未经博主允许不得转载. 题目描述: 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构). 解题思路: 首先看牛客网给出的测试用例: ...
- acwing 70-72 剑指OFFER 二叉树相关
地址 https://www.acwing.com/problem/content/66/ https://www.acwing.com/problem/content/67/ https://www ...
- 剑指Offer 树的子结构
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 思路: 分为2个部分.1先找出A中和B根节点相同的节点r. 2,咱判断B中所有孩子节点是不 ...
- 剑指Offer——树的子结构
题目描述: 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 分析: 先匹配到A的某个结点和B的根相同,然后往下继续匹配.不匹配则递归匹配左右子树. 代码: ...
- 用js刷剑指offer(树的子结构)
题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) 牛客网链接 js代码 /* function TreeNode(x) { this.val = x ...
- 剑指 offer 树的子结构
题目描述: 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构). 第一遍没写出来错误点:认为首先应该找到pRoot1等于pRoot2的节点,但是递归就是自己在不 ...
- 剑指offer 树的基本操作:四种遍历方式
前序遍历 递归版 编程思想 即借助系统栈,效率较低.二叉树的前序遍历规则:1. 访问根结点: 2. 遍历左子树: 3. 遍历右子树 编程实现 //树的定义 struct TreeNode { int ...
- 剑指offer——链表相关问题总结
首先统一链表的数据结构为: struct ListNode { int val; struct ListNode *next; ListNode(int x) :val(x), next(NULL) ...
- 【剑指offer】树的子结构
转载请注明出处:http://blog.csdn.net/ns_code/article/details/25907685 剑指offer第18题,九度OJ上測试通过! 题目描写叙述: 输入两颗二叉树 ...
- 剑指offer 判断树是不是对称的
html, body { font-size: 15px; } body { font-family: Helvetica, "Hiragino Sans GB", 微软雅黑, & ...
随机推荐
- osg坐标位置转换
osg::Vec3f vec3f1 = hookNode->getBound().center(); osg::NodePathList nodePAthList1 = hookNode-> ...
- scons 简单入门
scons 简单入门 摘自:https://www.jianshu.com/p/e4bd3ab9e5d6 0.2042019.01.20 12:58:44字数 1201阅读 3021 简单入门 hel ...
- Python subprocess中的run方法
调用subprocess的推荐方法是对于它可以处理的所有使用场景都使用run()函数. run()函数是在Python 3.5中添加的,如果在老版本中使用,需要下载并扩展. 扩展安装方式: $ pip ...
- python web 框架
Web框架(Web framework)是一种开发框架,用来支持动态网站.网络应用和网络服务的开发. wsgiref模块 wsgiref模块就是python基于wsgi协议(Web Server Ga ...
- 安卓apk反编译
在win环境反编译安卓APP的.apk文件 安卓apk 反编译为 Java源码图文教程 Android安全攻防战,反编译与混淆技术完全解析(上)
- 在 ServiceModel 客户端配置部分中,找不到引用协定“WebServiceSoap”的默认终结点元素。这可能是因为未找到应用程序的配置文件,或者是因为客户端元素找不到与此协定匹配的终结点元素(转)
按语: 在项目中实现自动升级过程,在类库中调用webservice取升级update.xml文件,添加服务调用,但在类库中调用时就出现异常,但在简单的测试工程中没有问题.解决方法采用下面介绍的方法 在 ...
- [ kvm ] 学习笔记 7:KVM 虚拟机创建的几种方式
通过对 qemu-kvm.libvirt 的学习,总结三种创建虚拟机的方式: (1)通过 qemu-kvm 创建 (2)通过 virt-install 创建 (3)通过 virt-manager 创建 ...
- python 求交集、并集、差集
需要用到set类型 交集,两种方法 retA = [i for i in listA if i in listB] retB = setA.intersection(setB) 并集 retC = s ...
- C#操作IIS程序池及站点的创建配置实现代码
首先要对Microsoft.Web.Administration进行引用,它主要是用来操作IIS7: using System.DirectoryServices;using Microsoft.We ...
- iOS-关于创建真机调试证书(发布证书,测试证书,推送调试证书)【转】