4.在二元树中找出和为某一值的所有路径(树)
2013-10-06 19:30
288 查看
题目:输入一个整数和棵二元树。
从树的根结点开始往下访问一直到叶所经过有形成条路径。
打印出和与输入整数相等的所有路径。
例如输入整数22 和如下二元树
则打印出两条路径:10, 12 和10, 5, 7。
参考微软100题的答案及http://www.cnblogs.com/qi09/archive/2011/05/24/2055643.html
整理思路如下:
1.创建二叉排序树;2.查找路径,FindPath(BinaryTreeNode *pCurrent,int ExpecteSum,vector<int> &Path,int CurrentSum)。
其中查找路径如下:1.访问到当前结点,把该结点添加到路径上,并累加当前结点的值,即CurrentSum+=pCurrent->m_nValue。
2.判断当前结点是叶子结点,同时ExpecteSum== CurrentSum,则将当前路径打印出来;
3.如果当前结点不是叶子结点,则依次访问该结点的左右孩子结点;
4.当当前结点访问结束后,递归函数将自动回到父结点,故在函数退出前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是根 结点到父结点的路径。
“我们不难看出保存路径的数据结构实际上是一个栈结构,因为路径要与递归调用状态一致,而递归调用本质就是一个压栈和出栈的过程。”
其中好多人都有vector<>来替代栈,但自己对C++并不熟悉,故在此先用下,以后有时间整理下C++。
void push_back(const T&x);//向容器末尾添加一个元素
void pop_back();//弹出容器中最后一个元素(窗口必须非空)
vector<int>::iterator iter;其中iterator表示游标,iter相当于i变量,但输出值为*iter。
从树的根结点开始往下访问一直到叶所经过有形成条路径。
打印出和与输入整数相等的所有路径。
例如输入整数22 和如下二元树
10
/ \
5 12
/ \
4 7
则打印出两条路径:10, 12 和10, 5, 7。
参考微软100题的答案及http://www.cnblogs.com/qi09/archive/2011/05/24/2055643.html
整理思路如下:
1.创建二叉排序树;2.查找路径,FindPath(BinaryTreeNode *pCurrent,int ExpecteSum,vector<int> &Path,int CurrentSum)。
其中查找路径如下:1.访问到当前结点,把该结点添加到路径上,并累加当前结点的值,即CurrentSum+=pCurrent->m_nValue。
2.判断当前结点是叶子结点,同时ExpecteSum== CurrentSum,则将当前路径打印出来;
3.如果当前结点不是叶子结点,则依次访问该结点的左右孩子结点;
4.当当前结点访问结束后,递归函数将自动回到父结点,故在函数退出前要在路径上删除当前结点并减去当前结点的值,以确保返回父结点时路径刚好是根 结点到父结点的路径。
“我们不难看出保存路径的数据结构实际上是一个栈结构,因为路径要与递归调用状态一致,而递归调用本质就是一个压栈和出栈的过程。”
其中好多人都有vector<>来替代栈,但自己对C++并不熟悉,故在此先用下,以后有时间整理下C++。
void push_back(const T&x);//向容器末尾添加一个元素
void pop_back();//弹出容器中最后一个元素(窗口必须非空)
vector<int>::iterator iter;其中iterator表示游标,iter相当于i变量,但输出值为*iter。
#include<stdio.h> #include<stdlib.h> #include<vector.h> struct BinaryTreeNode { int m_nValue; // value of node BinaryTreeNode *m_pLeft; //left child of node BinaryTreeNode *m_pRight; //rignt child of node }; void addBTreeNode(BinaryTreeNode *&pCurrent,int key) { if (pCurrent==NULL) { BinaryTreeNode *pBTree=(BinaryTreeNode *)malloc(sizeof(BinaryTreeNode)); pBTree->m_nValue=key; pBTree->m_pLeft=NULL; pBTree->m_pRight=NULL; pCurrent=pBTree; } else { if ((pCurrent->m_nValue) > key) { addBTreeNode(pCurrent->m_pLeft,key); } else { if ((pCurrent->m_nValue) < key) { addBTreeNode(pCurrent->m_pRight,key); } else { printf("Repeated!\n"); } } } } void FindPath(BinaryTreeNode *pCurrent, int Expectedsum,vector<int> &path,int Currentsum) { if (pCurrent==NULL) { return; } Currentsum+=pCurrent->m_nValue; path.push_back(pCurrent->m_nValue); //if the node is a leaf, and the sum is same as pre-defined, //the path is what we want. print the path bool leaf=(pCurrent->m_pLeft==NULL&&pCurrent->m_pRight==NULL); if (Expectedsum==Currentsum&&leaf) { vector<int>::iterator iter; for (iter=path.begin(); iter!=path.end(); iter++) { printf("%d ",*iter); } printf("\n"); } //if the node is not a leaf, goto its children if (pCurrent->m_pLeft) FindPath(pCurrent->m_pLeft,Expectedsum,path,Currentsum); if (pCurrent->m_pRight) FindPath(pCurrent->m_pRight,Expectedsum,path,Currentsum); //when we finish visiting a node and return to its parent node, //we should delete this node from the path and //minus the node's value from the current sum Currentsum-=pCurrent->m_nValue; path.pop_back(); } int main() { BinaryTreeNode *root=NULL; addBTreeNode(root,10); addBTreeNode(root,5); addBTreeNode(root,12); addBTreeNode(root,4); addBTreeNode(root,7); vector<int> path; FindPath(root,22,path,0); system("pause"); }
相关文章推荐
- flag2在二元树中找出和为某一值的所有路径 题目:输入一个整数和一棵二元树。 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 打印出和与输入整数相等的所有路径。 例如 输入整数
- 在二元树中找出和为某一值的所有路径
- 面试经典--在二元树中找出和为某一值的所有路径(树)
- [编程之美-03]在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- No4、在二元树中找出和为某一值的所有路径(树)
- 在二元树中找出和为某一值的所有路径
- .在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 【面试题】在二元树中找出和为某一值的所有路径——Java实现
- 在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 微软面试之4 在二元树中找出和为某一值的所有路径(树)
- 4.在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径
- 程序员面试题精选(4):在二元树中找出和为某一值的所有路径
- 程序员面试题精选100题(04)-在二元树中找出和为某一值的所有路径
- 在二元树中找出和为某一值的所有路径