Chinaunix首页 | 论坛 | 博客
  • 博客访问: 65266
  • 博文数量: 115
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-08 19:09
文章分类
文章存档

2015年(115)

我的朋友

分类: C/C++

2015-08-06 16:50:15

4.在二元树中找出和为某一值的所有路径
题目:输入一个整数和一棵二元树。
从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。
打印出和与输入整数相等的所有路径。
例如 输入整数22和如下二元树
  10   
  / \   
  5 12   
  / \   
  4 7
则打印出两条路径:10, 12和10, 5, 7。

这道题的递归显然是很简单,先序遍历,同时用一个栈保存所经过的路径,当和为指定值的时候就输出

非递归算法随便搜了下没有看到,费了比较大精力才写出来。
核心问题是对于非递归的先序遍历,存储路径的栈并不是存储的全路径,实际当走到某个节点的右结点时,该节点已经从出栈了,所以这个栈里路径是不对的,并不能使用。
解决方法有2个:
1.保存一个访问过的节点的列表,当读到一个节点时,进栈,保持栈中的内容就是全路径,当出栈后,判断栈顶节点的右边是不是已经访问过了,如没访问过,则说明这次该访右边了,反之,则继续出栈。
这个方法的问题是每次读到一个节点,就要遍历一次读过的所有节点,所以对于n个节点的树,其复杂度大概为n^2。这个逻辑比较简单,就不实现了。
2.是保存两个栈,一个s1用于先序遍历,一个s2用于保存从根到当前节点的路径。其复杂度仍然为线性,但是逻辑比较难懂
当读到某个cur节点的右节点时,说明cur以及cur的左边已经读过了,只需读右边部分,这个节点cur从s1出栈(下回只需要处理cur的父结点),但从s2并不出栈.
当cur的左右节点都遍历完成的时,这时s1指向的是cur的parent,s2指向的是cur的右节点。这时对s2出栈,一直到和s1的栈顶元素相同时,继续遍历右边的子树。


点击(此处)折叠或打开

  1. /*
  2.  * =====================================================================================
  3.  *
  4.  * Filename: btree.c
  5.  *
  6.  * Description: practice for binary tree
  7.  *
  8.  * Version: 1.0
  9.  * Created: 09/21/2012 09:28:18 PM
  10.  * Revision: none
  11.  * Compiler: gcc
  12.  *
  13.  * Author: royn.wang.renyuan@gmail.com
  14.  * Organization:
  15.  *
  16.  * =====================================================================================
  17.  */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <unistd.h>
  21. typedef struct tagNode{
  22.     int value;
  23.     struct tagNode* left;
  24.     struct tagNode* right;
  25. }Node;

  26. Node* CreateBTree2(Node* root, int* numbers, int size){
  27.     if(size == 0) return NULL;
  28.     printf ( "tracking %d\n", *numbers );
  29.     root = (Node*)malloc(sizeof(Node));
  30.     int mid = size/2;
  31.     root-> value = numbers[mid];
  32.     printf ( "fill %p : %d\n", root, root->value );
  33.     root->left = CreateBTree2(root, numbers, mid);
  34.     root->right = CreateBTree2(root, numbers+mid+1, size - mid -1);
  35.     return root;
  36. }

  37. void GetPathSumNR(Node *head, int tar){
  38.     Node **spath = (Node **)malloc(sizeof(void*)*1000);
  39.     Node **dpath = (Node **)malloc(sizeof(void*)*1000);
  40.     Node **shead = &spath[1];

  41.     *spath= *dpath = NULL;
  42.     int sum = 0;
  43.     
  44.     Node *root = head;
  45.     while(root || (*dpath)!=NULL){
  46.         while(root){
  47.             dpath++;
  48.             spath++;
  49.             *spath = root;
  50.             *dpath = root;
  51.             printf ( "push %d\n", root->value );
  52.             sum+=root->value;
  53.             if(sum == tar){
  54.                 Node **tmp = shead;
  55.                 while(*tmp){
  56.                     printf ( "%d -> ", (*tmp)->value );
  57.                     tmp++;
  58.                 }
  59.                 printf ( "\n" );
  60.             }
  61.             
  62.             root = root->left;
  63.         }
  64.         if(*dpath){
  65.             root = *dpath;
  66.             root = root->right;
  67.             *dpath =NULL;
  68.             dpath--;
  69.             if(root == NULL) {
  70.                 while(*spath != *dpath){
  71.                     sum-= (*spath)->value;
  72.                     printf ( "pop %d\n", (*spath)->value );
  73.                     *spath = NULL;
  74.                     spath--;
  75.                 }
  76.             }
  77.         }
  78.     }
  79. }


  80. void GetPathSum(Node * head, int tar, int sum, Node** path, Node **pathhead){
  81.     printf ( "tracking %d\n", head->value );
  82.     if(path == NULL){
  83.         path = (Node **)malloc(sizeof(void*)*1000);
  84.         pathhead = path;
  85.         path --;
  86.     }
  87.     path++;
  88.     *path = head;
  89.     sum += head->value;
  90.     if(sum == tar){
  91.         while(*pathhead){
  92.             printf ( "%d -> ", (*pathhead)->value );
  93.             pathhead++;
  94.         }
  95.         printf ( "\n" );
  96.     }

  97.     if(head->left){
  98.         GetPathSum(head->left, tar, sum, path, pathhead);
  99.     }
  100.     if(head->right){
  101.         GetPathSum(head->right, tar, sum, path, pathhead);
  102.     }
  103. }



  104. /*
  105.  * === FUNCTION ======================================================================
  106.  * Name: main
  107.  * Description:
  108.  * =====================================================================================
  109.  */
  110.     int
  111. main ( int argc, char *argv[] )
  112. {
  113.     //Create binary tree
  114.     int nums[]= {1,2,3,4,5,6,7,8,9,10};
  115.     printf ( "----------Creating tree----------\n" );
  116.     Node *head = CreateBTree2(NULL, nums, 10);
  117.     //Get the path from root to the node 3
  118.     
  119.     printf ( "------------sum = 15------------------\n" );
  120.     GetPathSumNR(head,15);


  121.     return EXIT_SUCCESS;
  122. } /* ---------- end of function main ---------- */




阅读(131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~