当前位置: 首页 > news >正文

求根到叶子节点数字之和

问题描述

给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字。

例如,从根到叶子节点路径 1->2->3 代表数字 123。

计算从根到叶子节点生成的所有数字之和。

说明: 叶子节点是指没有子节点的节点。

示例 1:

输入: [1,2,3]

    1

   / \

  2   3

输出: 25

解释:

从根到叶子节点路径 1->2 代表数字 12.

从根到叶子节点路径 1->3 代表数字 13.

因此,数字总和 = 12 + 13 = 25.

示例 2:

输入: [4,9,0,5,1]

    4

   / \

  9   0

 / \

5   1

输出: 1026

解释:

从根到叶子节点路径 4->9->5 代表数字 495.

从根到叶子节点路径 4->9->1 代表数字 491.

从根到叶子节点路径 4->0 代表数字 40.

因此,数字总和 = 495 + 491 + 40 = 1026.

可使用以下main函数:

#include <iostream>

#include <queue>

#include <cstdlib>

#include <cstring>

using namespace std;

struct TreeNode

{

    int val;

    TreeNode *left;

    TreeNode *right;

    TreeNode() : val(0), left(NULL), right(NULL) {}

    TreeNode(int x) : val(x), left(NULL), right(NULL) {}

    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}

};

TreeNode* inputTree()

{

    int n,count=0;

    char item[100];

    cin>>n;

    if (n==0)

        return NULL;

    cin>>item;

    TreeNode* root = new TreeNode(atoi(item));

    count++;

    queue<TreeNode*> nodeQueue;

    nodeQueue.push(root);

    while (count<n)

    {

        TreeNode* node = nodeQueue.front();

        nodeQueue.pop();

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {

            int leftNumber = atoi(item);

            node->left = new TreeNode(leftNumber);

            nodeQueue.push(node->left);

        }

        if (count==n)

            break;

        cin>>item;

        count++;

        if (strcmp(item,"null")!=0)

        {

            int rightNumber = atoi(item);

            node->right = new TreeNode(rightNumber);

            nodeQueue.push(node->right);

        }

    }

    return root;

}

int main()

{

    TreeNode* root;

    root=inputTree();

    int res=Solution().sumNumbers(root);

    cout<<res<<endl;

}

输入说明

首先输入结点的数目n(注意,这里的结点包括题中的null空结点)

然后输入n个结点的数据,需要填充为空的结点,输入null。

输出说明

输出一个整数(在32位int型数表示范围内),表示结果。

输入范例

5
4 9 0 5 1

输出范例

1026

实现思路

先序遍历树,用栈实现的非递归遍历,为记录路径,压入栈的元素是pair<TreeNode*,int>。int用来记录路径

实现代码
#include <iostream>#include <queue>#include <cstdlib>#include<stack>#include <cstring>using namespace std;struct TreeNode{int val;TreeNode *left;TreeNode *right;TreeNode() : val(0), left(NULL), right(NULL) {}TreeNode(int x) : val(x), left(NULL), right(NULL) {}TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}};TreeNode* inputTree(){int n,count=0;char item[100];cin>>n;if (n==0)return NULL;cin>>item;TreeNode* root = new TreeNode(atoi(item));count++;queue<TreeNode*> nodeQueue;nodeQueue.push(root);while (count<n){TreeNode* node = nodeQueue.front();nodeQueue.pop();cin>>item;count++;if (strcmp(item,"null")!=0){int leftNumber = atoi(item);node->left = new TreeNode(leftNumber);nodeQueue.push(node->left);}if (count==n)break;cin>>item;count++;if (strcmp(item,"null")!=0){int rightNumber = atoi(item);node->right = new TreeNode(rightNumber);nodeQueue.push(node->right);}}return root;}class Solution{
public:int sumNumbers(TreeNode *root){if(root==NULL)return 0;stack<pair<TreeNode*,int>>s;s.push(pair<TreeNode*,int>(root,root->val));int res = 0;while(!s.empty()){pair<TreeNode *,int>p = s.top();s.pop();if(!p.first->left&&!p.first->right) res+=p.second;if(p.first->right){s.push(pair<TreeNode*,int>(p.first->right,p.first->right->val+p.second*10));}if(p.first->left){s.push(pair<TreeNode*,int>(p.first->left,p.first->left->val+p.second*10));}}return res;}
};int main(){TreeNode* root;root=inputTree();int res=Solution().sumNumbers(root);cout<<res<<endl;}

http://www.dtcms.com/a/312777.html

相关文章:

  • 【数据分享】南京诗歌文学地理数据集(获取方式看文末)
  • 电机结构设计与特性曲线分析:基于MATLAB和FEMM的仿真研究
  • 6. 平台总线
  • 机器学习第四课之决策树
  • Shell 脚本流程控制语句详解(四):while 循环详解
  • lua table常用函数汇总
  • Django 序列化详解:从 Model 到 JSON,全面掌握数据转换机制
  • 使用AndroidStudio调试Framework源码
  • 腾讯人脸识别
  • 数据治理:DQC(Data Quality Center,数据质量中心)概述
  • [嵌入式embed]C51单片机STC-ISP提示:正在检测目标单片机
  • 《前端无障碍设计的深层逻辑与实践路径》
  • MyBatis动态SQL精要:从<if>到<foreach>的灵活拼接之道
  • 高质量数据集|建设三大难点
  • [硬件电路-140]:模拟电路 - 信号处理电路 - 锁定放大器概述、工作原理、常见芯片、管脚定义
  • [硬件电路-133]:模拟电路 - 信号处理电路 - 电荷放大器概述、工作原理、常见芯片、管脚定义
  • 深度学习(鱼书)day10--与学习相关的技巧(后两节)
  • 仿TCmalloc内存分配器
  • 后端研发转型爬虫实战:Scrapy 二开爬虫框架的避坑指南
  • C++入门自学Day5-- C/C++内存管理(续)
  • jvm之jconsole的使用
  • Maven 常用命令详解
  • react native中markdown添加数学公式的支持
  • 文明存续的时间博弈:论地球资源枯竭临界期的技术突围与行动紧迫性
  • STM32-驱动OLED显示屏使用SPI(软件模拟时序)实现
  • 【stm32】按键控制LED以及光敏传感器控制蜂鸣器
  • PYTHON从入门到实践-18Django模版渲染
  • 我的世界进阶模组开发教程——伤害(2)
  • 20250803让飞凌OK3576-C开发板在Rockchip的原厂Android14下适配声卡NAU88C22YG【Android部分】
  • C++:STL中的栈和队列的适配器deque