您的位置:首页 > 其它

题目:输入两棵二叉树A和B,判断B是不是A的子结构。

2017-07-09 15:18 465 查看
//判断是否为子树
#include <cstdio>

struct BinaryTreeNode
{
double                 m_dbValue;
BinaryTreeNode*        m_pLeft;
BinaryTreeNode*        m_pRight;
};

bool DoesTree1HaveTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2);
bool Equal(double num1, double num2);

bool HasSubtree(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2)
{
bool result = false;

if(pRoot1 != nullptr && pRoot2 != nullptr)
{
if(Equal(pRoot1->m_dbValue, pRoot2->m_dbValue))
result = DoesTree1HaveTree2(pRoot1, pRoot2);
if(!result)
result = HasSubtree(pRoot1->m_pLeft, pRoot2);
if(!result)
result = HasSubtree(pRoot1->m_pRight, pRoot2);
}

return result;
}

bool DoesTree1HaveTree2(BinaryTreeNode* pRoot1, BinaryTreeNode* pRoot2)
{
if(pRoot2 == nullptr)
return true;

if(pRoot1 == nullptr)
return false;

if(!Equal(pRoot1->m_dbValue, pRoot2->m_dbValue))
return false;

return DoesTree1HaveTree2(pRoot1->m_pLeft, pRoot2->m_pLeft) &&
DoesTree1HaveTree2(pRoot1->m_pRight, pRoot2->m_pRight);
}

bool Equal(double num1, double num2)
{
if((num1 - num2 > -0.0000001) && (num1 - num2 < 0.0000001))
return true;
else
return false;
}

// 题目:请完成一个函数,输入一个二叉树,该函数输出它的镜像。

#include <cstdio>
#include "..\Utilities\BinaryTree.h"
#include <stack>

void MirrorRecursively(BinaryTreeNode *pNode)
{
if((pNode == nullptr) || (pNode->m_pLeft == nullptr && pNode->m_pRight))
return;

BinaryTreeNode *pTemp = pNode->m_pLeft;
pNode->m_pLeft = pNode->m_pRight;
pNode->m_pRight = pTemp;

if(pNode->m_pLeft)
MirrorRecursively(pNode->m_pLeft);

if(pNode->m_pRight)
MirrorRecursively(pNode->m_pRight);
}

void MirrorIteratively(BinaryTreeNode* pRoot)
{
if(pRoot == nullptr)
return;

std::stack<BinaryTreeNode*> stackTreeNode;
stackTreeNode.push(pRoot);

while(stackTreeNode.size() > 0)
{
BinaryTreeNode *pNode = stackTreeNode.top();
stackTreeNode.pop();

BinaryTreeNode *pTemp = pNode->m_pLeft;
pNode->m_pLeft = pNode->m_pRight;
pNode->m_pRight = pTemp;

if(pNode->m_pLeft)
stackTreeNode.push(pNode->m_pLeft);

if(pNode->m_pRight)
stackTreeNode.push(pNode->m_pRight);
}
}


// 面试题30:包含min函数的栈
 // 题目:定义栈的数据结构,请在该类型中实现一个能够得到栈的最小元素的min
 // 函数。在该栈中,调用min、push及pop的时间复杂度都是O(1)。
template <typename T> class StackWithMin
{
public:
StackWithMin() {}
virtual ~StackWithMin() {}

T& top();
const T& top() const;

void push(const T& value);
void pop();

const T& min() const;

bool empty() const;
size_t size() const;

private:
std::stack<T>   m_data;     // 数据栈,存放栈的所有元素
std::stack<T>   m_min;      // 辅助栈,存放栈的最小元素
};

template <typename T> void StackWithMin<T>::push(const T& value)
{
// 把新元素添加到辅助栈
m_data.push(value);

// 当新元素比之前的最小元素小时,把新元素插入辅助栈里;
// 否则把之前的最小元素重复插入辅助栈里
if(m_min.size() == 0 || value < m_min.top())
m_min.push(value);
else
m_min.push(m_min.top());
}

template <typename T> void StackWithMin<T>::pop()
{
assert(m_data.size() > 0 && m_min.size() > 0);

m_data.pop();
m_min.pop();
}

template <typename T> const T& StackWithMin<T>::min() const
{
assert(m_data.size() > 0 && m_min.size() > 0);

return m_min.top();
}

template <typename T> T& StackWithMin<T>::top()
{
return m_data.top();
}

//判断是否为可能的出栈顺序
#include<iostream>
#include<stack>
#include<vector>
using namespace std;

bool isPopOrder(const vector<int>& vpush,const vector<int>& vpop){
stack<int> s;//辅助栈
int i=0,j=0;
while(j<vpop.size()){
while(s.empty()||s.top()!=vpop[j]){
if(i==vpush.size()) break;
s.push(vpush[i]);
i++;
}
if(s.top()!=vpop[j]) break;
s.pop();
j++;
}
return s.empty()&&j==vpop.size();

}
int main(){
vector<int> nums{1,2,3,4,5};
vector<int> order{4,3,5,2,1};
cout<<isPopOrder(nums,order);
}

// 题目:输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。
// 如果是则返回true,否则返回false。假设输入的数组的任意两个数字都互不相同。

#include <cstdio>

// BST:Binary Search Tree,二叉搜索树
bool VerifySquenceOfBST(int sequence[], int length)
{
if(sequence == nullptr || length <= 0)
return false;

int root = sequence[length - 1];

// 在二叉搜索树中左子树的结点小于根结点
int i = 0;
for(; i < length - 1; ++ i)
{
if(sequence[i] > root)
break;
}

// 在二叉搜索树中右子树的结点大于根结点
int j = i;
for(; j < length - 1; ++ j)
{
if(sequence[j] < root)
return false;
}

// 判断左子树是不是二叉搜索树
bool left = true;
if(i > 0)
left = VerifySquenceOfBST(sequence, i);

// 判断右子树是不是二叉搜索树
bool right = true;
if(i < length - 1)
right = VerifySquenceOfBST(sequence + i, length - i - 1);

return (left && right);
}


随机链表的深拷贝/**
* Definition for singly-linked list with a random pointer.
* struct RandomListNode {
* int label;
* RandomListNode *next, *random;
* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}
* };
*/
class Solution {
public:
RandomListNode *copyRandomList(RandomListNode *head) {
RandomListNode *newHead, *l1, *l2;
if (head == NULL) return NULL;
for (l1 = head; l1 != NULL; l1 = l1->next->next) {
l2 = new RandomListNode(l1->label);
l2->next = l1->next;
l1->next = l2;
}

newHead = head->next;
for (l1 = head; l1 != NULL; l1 = l1->next->next) {
if (l1->random != NULL) l1->next->random = l1->random->next;
}

for (l1 = head; l1 != NULL; l1 = l1->next) {
l2 = l1->next;
l1->next = l2->next;
if (l2->next != NULL) l2->next = l2->next->next;
}

return newHead;
}
};
// 面试题36:二叉搜索树与双向链表
// 题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求
// 不能创建任何新的结点,只能调整树中结点指针的指向。

#include <cstdio>
#include "..\Utilities\BinaryTree.h"

void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList);

BinaryTreeNode* Convert(BinaryTreeNode* pRootOfTree)
{
BinaryTreeNode *pLastNodeInList = nullptr;
ConvertNode(pRootOfTree, &pLastNodeInList);

// pLastNodeInList指向双向链表的尾结点,
// 我们需要返回头结点
BinaryTreeNode *pHeadOfList = pLastNodeInList;
while(pHeadOfList != nullptr && pHeadOfList->m_pLeft != nullptr)
pHeadOfList = pHeadOfList->m_pLeft;

return pHeadOfList;
}

void ConvertNode(BinaryTreeNode* pNode, BinaryTreeNode** pLastNodeInList)
{
if(pNode == nullptr)
return;

BinaryTreeNode *pCurrent = pNode;

if (pCurrent->m_pLeft != nullptr)
ConvertNode(pCurrent->m_pLeft, pLastNodeInList);

pCurrent->m_pLeft = *pLastNodeInList;
if(*pLastNodeInList != nullptr)
(*pLastNodeInList)->m_pRight = pCurrent;

*pLastNodeInList = pCurrent;

if (pCurrent->m_pRight != nullptr)
ConvertNode(pCurrent->m_pRight, pLastNodeInList);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐