//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.
//
// 译文:
//
// 给定一棵二叉树,每个结点包含一个值。打印出所有满足以下条件的路径: 路径上结点的值加起来等于给定的一个值。注意:这些路径不必从根结点开始。 #include <iostream>
#include <string>
#include <vector>
#include <stack> #include <stdlib.h>
using namespace std; class tree
{
private:
struct treenode
{
char data;
treenode * left;
treenode * right;
treenode * parent;
}; treenode *root; int index; void create(treenode **p, char *str, int i, int size, treenode *fa)
{ if (i>size-1 || str[i] == '\0')
{
*p = NULL;
return;
} if (str[i] == '#')
{
*p=NULL;
}
else
{
*p = new treenode;
(*p)->data = str[i];
(*p)->parent = fa;
create(&((*p)->left),str,2*i+1,size, *p);
create(&((*p)->right),str,2*i+2,size, *p);
}
} void pOrder(treenode *p)
{
if (p==NULL)
{
return;
} // cout<<p->data<<" "<<endl;
pOrder(p->left);
pOrder(p->right);
} void zOrder(treenode *p)
{
if (p==NULL)
{
return;
}
zOrder(p->left);
cout<<p->data<<" "<<endl;
zOrder(p->right);
} void hOrder(treenode *p)
{
if (p==NULL)
{
return;
}
hOrder(p->left);
cout<<p->data<<" "<<endl;
hOrder(p->right);
} /***遍历每一个节点,朝上找父节点,如果能得到相等的sum ,则将该节点传递给print()***/
void findpath(treenode *p, int sum)
{
//cout<<"findpath"<<endl;
if (p == NULL)
{
return;
}
int i = 0;
int temp = 0;
treenode *pi = p;
while(pi != NULL)
{
temp +=pi->data - '0'; if (temp == sum)
{
print(p, i);
}
pi = pi->parent;
i++;
}
findpath(p->left, sum);
findpath(p->right, sum);
} /***从节点朝父节点找,应反向打印***/
void print(treenode *h, int level)
{
if (h == NULL)
{
return ;
}
//cout<<"print------"<<level<<endl;
stack<int>s;
int i=0; while(i <= level && h != NULL)
{
s.push(h->data - '0');
h = h->parent;
i++;
} while(!s.empty())
{
cout<<s.top()<<" ";
s.pop();
}
cout<<endl;
} /***与上一方法类似,只是这里通过vector来记录每个节点上一层经过的路径***/
void findpath(treenode *p, int sum, vector<int>&v, int level)
{
if (p == NULL)
{
return;
}
v.push_back(p->data - '0');
int temp = 0;
for (int i = level; i>-1;i--)
{
temp += v[i];/***从数组后面向前插入***/
if (temp == sum)
{
print(v,i);
}
}
vector<int>vl(v),vr(v);
findpath(p->left,sum, vl, level+1);
findpath(p->right,sum, vr, level+1);
}
/***从下向上,打印的是从该节点向上i层的data***/
void print(vector<int>v, int i)
{
if (v.empty() || i<0)
{
return;
}
for (int j = i;j < v.size(); j++)
{
cout<<v[j]<<" ";
}
cout<<endl;
} public: tree()
{
//root = create();
root = NULL;
index = 0;
} /***输入扩展层次遍历序列,#表示该节点为空***/
tree(char *s)
{
root = NULL;
index = 0;
if (s == NULL)
{
return;
} int size = strlen(s);
create(&root,s,0,size,NULL);
} ~tree()
{
/***清空二叉树***/
} void printPathSum(int sum)
{
findpath(root, sum);
} void printPathSum2(int sum)
{
vector<int>v;
findpath(root, sum,v,0);
} void preOrder(){pOrder(root);}
void inOreder(){zOrder(root);}
void postOreder(){ hOrder(root);}
}; int main()
{
/***扩展层次序列简立树***/
char t[14] = "1234#54#6##23";
tree s(t);
//s.preOrder();
s.printPathSum(10);
cout<<"xxxx"<<endl;
s.printPathSum2(10); cout<<"Over"<<endl;
return 0;
}

Cracking The Coding Interview4.8的更多相关文章

  1. Cracking The Coding Interview4.5

    //原文: // // Write an algorithm to find the 'next' node (i.e., in-order successor) of a given node in ...

  2. Cracking The Coding Interview4.3

    //Given a sorted (increasing order) array, write an algorithm to create a binary tree with minimal h ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  5. Cracking the coding interview--问题与解答

    http://www.hawstein.com/posts/ctci-solutions-contents.html 作者:Hawstein出处:http://hawstein.com/posts/c ...

  6. 《cracking the coding intreview》——链表

    前言 最近准备暑假回家回家修整一下,所以时间大部分用来完成项目上的工作,同时为了9月份的校招,晚上的时间我还在学习<cracking the coding intreview>,第二章链表 ...

  7. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  8. Cracking the Coding Interview(Stacks and Queues)

    Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...

  9. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

随机推荐

  1. Nginx自学笔记

    Nginx相关 标签(空格分隔): nginx 享学 安装部署 通过源代码的方式安装 使用 ./sbin/nginx #启动 ./sbin/nginx -t #检查是否有错 ./sbin/nginx ...

  2. centos7: 将nginx,php-fpm加入开机启动

    1. 自己新建一个脚本,如centnet-service.sh 经过后面的几个步骤后,这个脚本在开机的时候会执行,在这个脚本里面可以写你开机的时候想执行的命令,如启动nginx,phpf-pm等服务 ...

  3. c++-pimer-plus-6th-chapter03

    Chapter Review Having more than one integer type lets you choose the type that is best suited to a p ...

  4. Tomcat基本组件、其功能和处理请求的过程

      一.Tomcat是一个基于组件的服务器,它的构成组件都是可配置的,其中最外层的组件是Catalina Servlet容器,其他的组件按照一定的格式要求配置在这个顶层容器中 Tomcat的各个组件是 ...

  5. Spring Batch 使用场景

    一个标准的批处理程序通常会从数据库,文件或者队列中读取大量的数据和记录,然后对获取的数据进行处理,然后将修改后的格式写回到数据库中. 通常 Spring Batch 在离线模式下进行工作,不需要用户干 ...

  6. 框架中如何根据fileupload工具包实现文件上传功能

    工具包 Apache-fileupload.jar – 文件上传核心包. Apache-commons-io.jar – 这个包是fileupload的依赖包.同时又是一个工具包. 代码 servle ...

  7. Luffy之Xadmin以及首页搭建(轮播图,导航)

    1. 首页 1.1 轮播图 admin站点配置支持图片上传 pip install Pillow 默认情况下,Django会将上传的图片保存在本地服务器上,需要配置保存的路径.我们可以将上传的文件保存 ...

  8. MVC实战之排球计分(一)—— 需求分析与数据库设计

    此系列博客目的是制作一款排球计分程序.这系列博客讲讲述此软件的 各个功能的设计与实现. 一.需求分析: 这个程序是排球计分程序,其业务非常简单,具体如下: 1.本程序可以选择用户身份,通过不同角度记录 ...

  9. array 数组去重 过滤空值等方法

    去重操作 第一种方式, ES 6 引入的新书据结构 Set 本身就是没有重复数据的, 可以使用这个数据结构来转化数组.时间复杂度 O(n) 123456 const target = [];const ...

  10. CRM 价格批导

    日了,好多代码....COPY别人的,懒得改了 *----------------------------------------------------------------------* *** ...