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


题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如 输入整数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;
}
 

 

设计包含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;
}