zoukankan      html  css  js  c++  java
  • IT公司100题-4-在二元树中找出和为某一值的所有路径

    问题描述:

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

    例如输入整数30和如下二元树
     
    14
    /
    5 16
    /
    3 11
    则打印出两条路径:14, 16 和14, 5, 11。

    二元树节点的数据结构定义为:

    typedef struct BSTree {
        int data;
        BSTree* left;
        BSTree* right;
    } tree_node;

    在遍历树的过程中,使用stack保存所走过的路径。如果当前节点为叶子节点,并且路径和等于输入的整数,则输出路径。如果当前节点不是叶子节点,则递归的访问它的孩子节点。在回溯的过程中,注意路径的出栈。

    代码实现:

     1 // 4.cc
     2 #include <iostream>
     3 #include <vector>
     4 using namespace std;
     5 
     6 // 二叉查找树
     7 typedef struct BSTree {
     8   int data;
     9   BSTree* left;
    10   BSTree* right;
    11 } tree_node;
    12 
    13 // 创建二元查找树
    14 void add_BSTree_node(tree_node* &p_current, int data) {
    15   if (NULL == p_current) {
    16     tree_node *node = new tree_node();
    17     node->left = NULL;
    18     node->right = NULL;
    19     node->data = data;
    20     p_current = node;
    21   } else {
    22     if (p_current->data > data)
    23       add_BSTree_node(p_current->left, data);
    24     else if (p_current->data < data)
    25       add_BSTree_node(p_current->right, data);
    26     else
    27       cout << "The data has already in the tree.";
    28   }
    29 }
    30 
    31 void find_path(tree_node* root, int target, vector<int>& path, int& current_sum) {
    32   if(!root)
    33     return;
    34 
    35   current_sum += root->data;
    36   path.push_back(root->data);
    37 
    38   // 是叶子节点,并且等于target
    39   bool is_leaf = (!root->left && !root->right);
    40   if(current_sum == target && is_leaf) {
    41     for(vector<int>::iterator it = path.begin(); it != path.end(); ++it)
    42       cout << *it << " ";
    43     cout << endl;
    44   }
    45 
    46   // 非叶子节点,遍历孩子
    47   if(root->left)
    48     find_path(root->left, target, path, current_sum);
    49   if(root->right)
    50     find_path(root->right, target, path, current_sum);
    51 
    52   // pop当前元素
    53   current_sum -= root->data;
    54   path.pop_back();
    55 }
    56 
    57 int main() {
    58   tree_node *root = NULL;
    59 
    60   add_BSTree_node(root, 14);
    61   add_BSTree_node(root, 16);
    62   add_BSTree_node(root, 5);
    63   add_BSTree_node(root, 3);
    64   add_BSTree_node(root, 11);
    65 
    66   vector<int> path;
    67   int target = 30;
    68   int current_sum = 0;
    69   find_path(root, target, path, current_sum);
    70 
    71   return 0;
    72 }

    转载自源代码

    本文链接地址: http://w.worthsee.com/index.php/4-%e5%9c%a8%e4%ba%8c%e5%85%83%e6%a0%91%e4%b8%ad%e6%89%be%e5%87%ba%e5%92%8c%

  • 相关阅读:
    AT4119[ARC096C]Everything on It【斯特林数,容斥】
    AT2164[AGC006C]Rabbit Exercise【差分,倍增,数学期望】
    hdu5909Tree Cutting【FWT】
    JavaWeb apache和tomcat是如何配合工作的
    JavaWeb 目录
    SQL Server CLUSTERED
    SQL Server ISNULL
    JavaWeb Servlet教程
    SQL Server DISTINCT
    SQL Server 哈希索引
  • 原文地址:https://www.cnblogs.com/dracohan/p/3875194.html
Copyright © 2011-2022 走看看