zoukankan      html  css  js  c++  java
  • 【面试题】在二元树中找出和为某一值的所有路径

    题目:在二元树中找出和为某一值的所有路径

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

    思路:

    1、当访问到某一节点时,把该结点的值添加到当前和变量,且把该结点压入栈中。

    2、若结点为叶子结点,且当前和变量==期望的和,则打印栈中的结点值,即为所需的路径。

    3、若结点不是叶子结点,继续访问它的左孩子结点,访问它的右孩子结点。

    4、删除该结点。包括从当前和变量中减去结点值,从栈中弹出结点值。此时,已回到父结点。

    程序中的栈,利用STL中的vector,这样简化了编码工作。

    具体的findPath函数代码如下:

    void findPath(BinaryTreeNode *pTreeNode,
    int expectedSum,
    vector<int>& path,
    int & currentSum)
    {
    if( !pTreeNode )
    return;
    //结点值添加至当前和变量中
    //结点压入栈中
    currentSum += pTreeNode->m_nValue;
    path.push_back(pTreeNode->m_nValue);
    bool isLeaf = !(pTreeNode->m_pLeft) && !(pTreeNode->m_pRight);
    //当前结点为叶子结点,且和为期望值,打印路径
    if(currentSum == expectedSum && isLeaf)
    {
    vector<int>::iterator iter;
    for(iter = path.begin(); iter != path.end(); iter++)
    {
    cout << *iter << "\t";
    }
    cout << endl;
    }
    //当前结点不为叶子结点,查找它的左右孩子结点
    if(pTreeNode->m_pLeft)
    findPath(pTreeNode->m_pLeft, expectedSum, path, currentSum);
    if(pTreeNode->m_pRight)
    findPath(pTreeNode->m_pRight, expectedSum, path, currentSum);
    //当前结点删除,退至其父结点
    currentSum -= pTreeNode->m_nValue;
    path.pop_back();
    }

    完整的测试cpp文件如下:

    1 #include <iostream>
    2 #include <vector>
    3 #include <stdlib.h>
    4
    5  using namespace std;
    6
    7  struct BinaryTreeNode
    8 {
    9 int m_nValue;
    10 BinaryTreeNode * m_pLeft;
    11 BinaryTreeNode * m_pRight;
    12 };
    13
    14  int count = 0;
    15
    16  void findPath(BinaryTreeNode *pTreeNode,
    17 int expectedSum,
    18 vector<int>& path,
    19 int & currentSum)
    20 {
    21 if( !pTreeNode )
    22 return;
    23 currentSum += pTreeNode->m_nValue;
    24 path.push_back(pTreeNode->m_nValue);
    25 bool isLeaf = !(pTreeNode->m_pLeft) && !(pTreeNode->m_pRight);
    26 if(currentSum == expectedSum && isLeaf)
    27 {
    28 vector<int>::iterator iter;
    29 for(iter = path.begin(); iter != path.end(); iter++)
    30 {
    31 cout << *iter << "\t";
    32 }
    33 cout << endl;
    34 }
    35 if(pTreeNode->m_pLeft)
    36 findPath(pTreeNode->m_pLeft, expectedSum, path, currentSum);
    37 if(pTreeNode->m_pRight)
    38 findPath(pTreeNode->m_pRight, expectedSum, path, currentSum);
    39
    40 currentSum -= pTreeNode->m_nValue;
    41 path.pop_back();
    42 }
    43
    44  void addTree(BinaryTreeNode **T, int num)
    45 {
    46 if(*T == NULL)
    47 {
    48 *T = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode));
    49 (*T)->m_nValue = num;
    50 (*T)->m_pLeft = NULL;
    51 (*T)->m_pRight = NULL;
    52 }
    53 else if((*T)->m_nValue > num)
    54 addTree(&((*T)->m_pLeft), num);
    55 else if((*T)->m_nValue < num)
    56 addTree(&((*T)->m_pRight), num);
    57 else
    58 cout << "重复加入同一结点" << endl;
    59 }
    60
    61  int main()
    62 {
    63 BinaryTreeNode * T = NULL;
    64 addTree(&T, 10);
    65 addTree(&T, 12);
    66 addTree(&T, 5);
    67 addTree(&T, 7);
    68 addTree(&T, 4);
    69
    70 vector<int> path;
    71 int sum = 0;
    72 findPath(T, 22, path, sum);
    73
    74 return 0;
    75 }
  • 相关阅读:
    k8s keepalived haproxy 集群成功
    .Net Core 用 EntityFramework 读取 Oracle
    Fedora CoreOS 安装 非LInux专业国内第一手
    Docker 下的 Keepalived + Haproxy 高可用实现 1 实现结果演示
    第二篇 windows container 微软的原生容器
    第一篇 Windows docker 概述
    测试openLiveWrite写博客
    比特币勒索病毒肆虐,腾讯云安全专家给你支招
    WannaCry 勒索病毒用户处置指南
    pytorch 学习笔记之编写 C 扩展,又涨姿势了
  • 原文地址:https://www.cnblogs.com/qi09/p/2055643.html
Copyright © 2011-2022 走看看