在二元树中找出和为某一值的所有路径


题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如 输入整数22和如下二元树
  10  
  / /   
 5  12   
 /   /   
4     7
则打印出两条路径:10, 12和10, 5, 7。
 
二叉树节点的数据结构定义为:
struct BinaryTreeNode // a node in the binary tree
{
    int m_nValue; // value of node
    BinaryTreeNode *m_pLeft; // left child of node
    BinaryTreeNode *m_pRight; // right child of node
};
 
访问到某一个节点时,将该节点的值加到当前值,并将该节点加到路径上,如果当前值等于所求值,判断当前节点是否叶节点,若是则输出路径,否则返回父节点;如果当前值小于所求值,则继续搜索孩子节点。如果当前值大于所求值,则返回父节点。返回父节点前记得将该节点从路径中删除,并把当前值减去该节点的值。
#include <iostream>
#include <vector>
using namespace std;

struct BinaryTreeNode
{
    int m_nValue;
    BinaryTreeNode *m_pLeft;
    BinaryTreeNode *m_pRight;
}n1,n2,n3,n4,n5;

BinaryTreeNode* buildTree()
{
    n1.m_nValue = 10;
    n1.m_pLeft = &n2;
    n1.m_pRight = &n3;
    n2.m_nValue = 5;
    n2.m_pLeft = &n4;
    n2.m_pRight = &n5;
    n3.m_nValue = 12;
    n4.m_nValue = 4;
    n5.m_nValue = 7;
    n3.m_pLeft = n3.m_pRight = n4.m_pLeft = n4.m_pRight = n5.m_pLeft = n5.m_pRight = NULL;
    return &n1;
}

void FindPath(BinaryTreeNode *pCurrent,int sum,int curval,vector<int> &path)
{
    if(pCurrent == NULL)
    {
        return ;
    }
    curval += pCurrent->m_nValue;
    path.push_back(pCurrent->m_nValue);
    if(curval == sum)
    {
        if(pCurrent->m_pLeft == NULL && pCurrent->m_pRight == NULL)
        {
            for(vector<int>::iterator it = path.begin();it != path.end();it++)
            {
                cout<<*it<<" ";
            }
            cout<<endl;
        }
    }
    else 
    {
        if(pCurrent->m_pLeft)
        {
            FindPath(pCurrent->m_pLeft,sum,curval,path);
        }
        if(pCurrent->m_pRight)
        {
            FindPath(pCurrent->m_pRight,sum,curval,path);
        }
    }        
    curval -= pCurrent->m_nValue;
    path.pop_back();
}

int main()
{
    BinaryTreeNode *pRoot;
    vector<int> path;
    
    pRoot = buildTree();
    FindPath(pRoot,22,0,path);
    
    system("pause");
    return 0;
}
 

 

求子数组的最大和

题目:

输入一个整形数组,数组里有正数也有负数。

数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和。

求所有子数组的和的最大值。要求时间复杂度为O(n)。

例如输入的数组为1, -2, 3, 10, -4, 7, 2, -5,和最大的子数组为3, 10, -4, 7, 2,

因此输出为该子数组的和18。

 

题目只要求连续最大子段和,这很简单,扫描一遍整个数组,将数组元素一个个加起来,如果大于当前最大和,则更新最大和,否则不更新,如果最大和变为负数,则将最大和置零,因为连续最大子段和不可能包含使当前最大和为负的元素。

 

#include <iostream>
using namespace std;

int getMaxSum(int num[],int n)
{
    int sum,max;
    sum = max = 0;
    for(int i = 0;i < n;i++)
    {
        sum+=num[i];
        if(sum > max)max = sum;
        if(sum < 0)sum = 0;
    }
    return max;
}
int main()
{
    int a[10] = {1,-8,6,3,-1,5,7,-2,0,1};
    
    cout<<getMaxSum(a,10)<<endl;
    
    system("pause");
    return 0;
}

 

设计包含min函数的栈

题目:

定义栈的数据结构,要求添加一个min函数,能够得到栈的最小元素。

要求函数min、push以及pop的时间复杂度都是O(1)。

 

设置一个辅助栈,记录当前栈的最小元素,每当一个元素入栈,就判断该元素是否比当前最小元素小,如果是,则更新最小元素,否则不更新。有些同学可能会说用一个变量记录最小元素不就可以了吗,如果这样的话,万一出栈的正好是最小元素,那更新最小元素的时候就要再次搜索整个栈,时间复杂度就不是O(1)了。

 

#include <iostream>
using namespace std;

const int STACK_LEN = 100;

struct node
{
    int val;
    int min;
};

struct stack
{
    node data[STACK_LEN];
    int top;
};

void push(stack *stack,int val)
{
    if(stack->top < STACK_LEN)
    {
        stack->data[stack->top].val = val;
        if(stack->top > 0)
        {
            if(val < stack->data[stack->top-1].min)
            {
                stack->data[stack->top].min = val;
            }
            else 
            {
                stack->data[stack->top].min = stack->data[stack->top-1].min;
            }
        }
        else
        {
            stack->data[stack->top].min = val;
        }
        stack->top++;
    }
}

int pop(stack *stack)
{
   return stack->data[--stack->top].val; 
}

int min(stack *stack)
{
    return stack->data[stack->top-1].min;
}

int main()
{
    stack *s = new stack();
    push(s,2);
    push(s,6);
    push(s,4);
    push(s,1);
    push(s,5);
    
    while(s->top > 0)
    {
        cout<<min(s)<<endl;
        pop(s);
    }
    
    system("pause");
    return 0;
} 

把二叉查找树转变成排序的双向链表

 

题目:

输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。

要求不能创建任何新的结点,只调整指针的指向。

10

/ \

6 14

/ \ / \

4 8 12 16

转换成双向链表

4=6=8=10=12=14=16。

首先我们定义的二元查找树节点的数据结构如下:

struct BSTreeNode

{

int m_nValue; // value of node

BSTreeNode *m_pLeft; // left child of node

BSTreeNode *m_pRight; // right child of node

};

 

这个题不难,遍历一下即可,需要注意的是指针的链接不要出错。

 

#include <iostream>
using namespace std;

struct BSTreeNode
{
    int m_nValue;
    BSTreeNode *m_pLeft;
    BSTreeNode *m_pRight;
};

typedef BSTreeNode DeList;
DeList *head;
DeList *index;

//构造二叉树 
void buildBSTree(BSTreeNode *(&pCurrent),int value)
{
    if(pCurrent == NULL)
    {
        pCurrent = new BSTreeNode();
        pCurrent->m_pLeft = NULL;
        pCurrent->m_pRight = NULL;
        pCurrent->m_nValue = value;
    }
    else 
    {
        if(value < pCurrent->m_nValue)
        {
            buildBSTree(pCurrent->m_pLeft,value);
        }
        else if(value > pCurrent->m_nValue)
        {
            buildBSTree(pCurrent->m_pRight,value);
        }
    }
}

//转换成双向链表 
void convertToDeList(BSTreeNode *pCurrent)
{
    pCurrent->m_pLeft = index;
    if(index != NULL)
    {
        index->m_pRight = pCurrent;
    }
    else
    {
        head = pCurrent;
    }
    index = pCurrent;
    cout<<pCurrent->m_nValue<<endl;
}

//中序遍历二叉树 
void inOrder(BSTreeNode *pCurrent)
{
    if(pCurrent != NULL)
    {
        inOrder(pCurrent->m_pLeft);
        convertToDeList(pCurrent);
        inOrder(pCurrent->m_pRight);
    }
}

int main()
{
    BSTreeNode *pRoot = NULL;
    head = index = NULL;
    
    buildBSTree(pRoot,10);
    buildBSTree(pRoot,4);
    buildBSTree(pRoot,6);
    buildBSTree(pRoot,8);
    buildBSTree(pRoot,14);
    buildBSTree(pRoot,15);
    buildBSTree(pRoot,16);
    
    inOrder(pRoot);
    system("pause");
    
    return 0;
}