zoukankan      html  css  js  c++  java
  • 求二叉树中和为给定值的所有路径

    转自:http://blog.csdn.net/lalor/article/details/7614381

    问题定义:

            You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up to that value. Note that it can be any path in the tree-it does not have to start at the root.

    解题思路:

          一层一层的遍历,保存当前节点到根节点的完整路径,然后从当前节点向上扫描,如果找到了当前节点到某个节点的和等于给定值,则输出之。程序对每个节点都需要遍历一遍,还要扫描当前节点到根节点的路径,且需要保存每个节点到根节点的路径,所以时间复杂度为O(nlgn),空间复杂度为O(nlgn)。(ps:关于本程序中建树部分,可以参考:http://blog.csdn.net/lalor/article/details/7613621

    代码实例:

    #include <algorithm>  
    #include <iostream>  
    #include <time.h>  
    #include <assert.h>  
    #include <stdio.h>  
    #include <vector>  
      
      
    using namespace std;  
      
    struct node   
    {  
        int data;  
        struct node * lchild;  
        struct node * rchild;  
    };  
      
      
    //将数组转换为深度最低的二叉树,采用了二分查找的思想  
    struct node* ConvertArrayToTree(int data[], int first, int last)  
    {  
        if (last < first)   
        {  
            return NULL;  
        }  
        else  
        {  
            int mid = ( last + first ) / 2;  
            struct node * newNode = NULL;  
            newNode = (struct node *)malloc(sizeof(struct node));  
            newNode->data = data[mid];  
            newNode->lchild = ConvertArrayToTree(data, first, mid - 1);  
            newNode->rchild = ConvertArrayToTree(data, mid + 1, last);  
            return newNode;  
        }  
    }  
      
    //再最左边插入一个节点  
    void InsertNodeAtLeft(struct node *root, struct node *newNode)  
    {  
        assert(root != NULL && newNode != NULL);  
        while(root->lchild != NULL)  
        {  
            root = root->lchild;  
        }  
        root->lchild = newNode;  
    }  
      
    //在最右边插入一个节点  
    void InsertNodeAtRight(struct node *root, struct node *newNode)  
    {  
        assert(root != NULL && newNode != NULL);  
        while(root->rchild != NULL)  
        {  
            root = root->rchild;  
        }  
        root->rchild = newNode;  
    }  
    //中序遍历  
    void Traverse(struct node *root)  
    {  
        if (root == NULL)   
        {  
            return;  
        }  
        Traverse(root->lchild);  
        Traverse(root->rchild);  
        printf("%d	", root->data);  
    }  
      
    //打印和为sum的路径  
    void print(vector<int>& buffer, int first, int last)  
    {  
        int i;  
        for (i = first; i <= last; i++)   
        {  
            cout << buffer[i] << "	";  
        }  
        cout << endl;  
    }  
    void findSum(struct node *head, int sum, vector<int> &buffer, int level)  
    {  
        if (head == NULL) return;  
      
        int i;  
        int tmp = sum;  
        buffer.push_back(head->data);  
        for (i = level; i >= 0; i--)  
        {  
            tmp -= buffer[i];  
            if (tmp == 0) print(buffer, i, level);  
        }  
      
        vector<int> lbuffer(buffer);  
        vector<int> rbuffer(buffer);  
      
        findSum(head->lchild, sum, lbuffer, level + 1);  
        findSum(head->rchild, sum, rbuffer, level + 1);  
    }  
      
    int main(int argc, char* argv[])  
    {  
        const int SIZE = 10;//测试的数据量  
        int data[SIZE];//保存数据  
        int i, j;  
        struct node *head = NULL;  
      
        for (i = 0; i < SIZE; i++)   
        {  
            data[i] = i + 1;  
        }  
      
        head = ConvertArrayToTree(data, 0, SIZE - 1);  
      
        struct node *one = (struct node *)malloc(sizeof(struct node));  
        struct node *two = (struct node *)malloc(sizeof(struct node));  
        one->data = 11;  
        one->lchild = NULL;  
        one->rchild = NULL;  
          
        two->data = 4;  
        two->lchild = NULL;  
        two->rchild = NULL;  
      
        InsertNodeAtLeft(head, one);  
        InsertNodeAtRight(head, two);  
        //遍历数据  
    //  Traverse(head);  
    //  printf("
    ");  
        vector<int> v;  
        findSum(head, 14, v, 0);  
        return 0;  
    } 

    该示例中所使用的二叉树如下所示:

    运行结果如下:

  • 相关阅读:
    windows下查看端口进程占用情况
    分布式文件管理系统MooseFS在centOS 7中的安装
    开发中的技术选型调研总结
    windows和linux无法访问VMware中linux的tomcat主页问题
    如何使windows系统ping通VMware下面的linux系统
    VMware中linux安装jdk
    mysql数据库之存储过程入门
    Hibernate批处理操作优化 (批量插入、更新与删除)
    JDBC: 批量处理提高SQL处理速度
    小程序 长按复制文本
  • 原文地址:https://www.cnblogs.com/theCambrian/p/3934333.html
Copyright © 2011-2022 走看看