程序员面试题精选100题(04)-在二元树中找出和为某一值的所有路径

来源:互联网 发布:阿里云备案号 编辑:程序博客网 时间:2024/05/18 09:07

 http://zhedahht.blog.163.com/blog/static/254111742007228357325/

 

 

题目:输入一个整数和一棵二元树。从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。打印出和与输入整数相等的所有路径。

例如输入整数22和如下二元树

                                            10
                                           /   /
                                          5     12
                                        /   /   
                                      4     7 

则打印出两条路径:10, 1210, 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
};