LeetCode //C - 257. Binary Tree Paths

server/2024/10/21 23:01:25/

257. Binary Tree Paths

Given the root of a binary tree, return all root-to-leaf paths in any order.

A leaf is a node with no children.
 

Example 1:

在这里插入图片描述

Input: root = [1,2,3,null,5]
Output: [“1->2->5”,“1->3”]

Example 2:

Input: root = [1]
Output: [“1”]

Constraints:
  • The number of nodes in the tree is in the range [1, 100].
  • -100 <= Node.val <= 100

From: LeetCode
Link: 257. Binary Tree Paths


Solution:

Ideas:

1. TreeNode Structure: The structure for a binary tree node is defined with integer value, left child, and right child pointers.

2. DFS Helper Function: The dfs function performs a depth-first search to find all paths from the root to the leaves. It constructs the path as a string and adds it to the paths array when a leaf is encountered.

3. Main Function: The binaryTreePaths function initializes the paths array and calls the dfs function to populate it. It also sets the return size of the paths array.

4. Helper Functions:

  • newNode creates a new tree node.
  • freeTree frees the allocated tree nodes.
  • freePaths frees the allocated paths array.
Code:
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     struct TreeNode *left;*     struct TreeNode *right;* };*/
/*** Note: The returned array must be malloced, assume caller calls free().*/
void dfs(struct TreeNode* root, char* path, int length, char** paths, int* size) {if (root == NULL) return;int len = snprintf(path + length, 100 - length, "%d", root->val);length += len;if (root->left == NULL && root->right == NULL) {paths[*size] = (char*)malloc((length + 1) * sizeof(char));strcpy(paths[*size], path);(*size)++;} else {length += snprintf(path + length, 100 - length, "->");dfs(root->left, path, length, paths, size);dfs(root->right, path, length, paths, size);}
}char** binaryTreePaths(struct TreeNode* root, int* returnSize) {char** paths = (char**)malloc(100 * sizeof(char*));char path[100];*returnSize = 0;if (root != NULL) {dfs(root, path, 0, paths, returnSize);}return paths;
}// Helper function to create a new tree node
struct TreeNode* newNode(int data) {struct TreeNode* node = (struct TreeNode*)malloc(sizeof(struct TreeNode));node->val = data;node->left = NULL;node->right = NULL;return node;
}// Helper function to free the allocated tree nodes
void freeTree(struct TreeNode* root) {if (root == NULL) return;freeTree(root->left);freeTree(root->right);free(root);
}// Helper function to free the paths array
void freePaths(char** paths, int size) {for (int i = 0; i < size; i++) {free(paths[i]);}free(paths);
}

http://www.ppmy.cn/server/90159.html

相关文章

昇思MindSpore 应用学习-GAN图像生成-CSDN

模型简介 生成式对抗网络(Generative Adversarial Networks&#xff0c;GAN)是一种生成式机器学习模型&#xff0c;是近年来复杂分布上无监督学习最具前景的方法之一。 最初&#xff0c;GAN由Ian J. Goodfellow于2014年发明&#xff0c;并在论文Generative Adversarial Nets中…

docker常用命令总结

Docker 的常用命令可以大致分为几大类&#xff1a;启动类、镜像类、容器类、网络类以及其他一些辅助命令。以下是对这些常用命令的详细归纳&#xff1a; 一、启动类命令 启动 Docker&#xff1a;systemctl start docker关闭 Docker&#xff1a;systemctl stop docker重启 Doc…

Redisson分布式锁使用详解

引言 日常开发中&#xff0c;难免遇到一些并发的场景&#xff0c;为了保证接口执行的一致性&#xff0c;通常采用加锁的方式&#xff0c;因为服务是分布式部署模式&#xff0c;本地锁Reentrantlock和Synchnorized这些就先放到一边了&#xff0c;Redis的setnx锁存在无法抱保证原…

Spring、SpringMVC、SpringBoot之间有什么关系?

Spring、SpringMVC、SpringBoot之间有什么关系&#xff1f; Spring通常是指Spring框架&#xff08;SpringFramework&#xff09;是一款开源的轻量级的JavaEE开发框架&#xff0c;旨在简化Java项目的开发。 SpringFramework中包含很多模块&#xff0c;包括IOC控制反转、AOP面向…

MySQL4.索引及视图

1.建库 create database mydb15_indexstu; use mydb15_indexstu;2.建表 2.1 student表学&#xff08;sno&#xff09;号为主键&#xff0c;姓名&#xff08;sname&#xff09;不能重名&#xff0c;性别&#xff08;ssex&#xff09;仅能输入男或女&#xff0c;默认所在系别&a…

Window下安装Zookeeper

一、下载 地址&#xff1a;https://archive.apache.org/dist/zookeeper/zookeeper-3.5.6/ 解压&#xff1a;非中文、没有空格目录下 新建data目录&#xff0c;用于存放数据文件 二、配置 进入conf目录&#xff0c;复制zoo_sample.cfg 为zoo.cfg 打开zoo.cfg 修改dataDir&…

帆软FineReport之替换函数

在日常帆软FineReport中经常会使用字符串替换函数&#xff0c;记录下来&#xff0c;方便备查。 一、字符串替换 第一种、指定文本替换 使用SUBSTITUTE函数&#xff0c;语法如下所示 SUBSTITUTE(text&#xff0c;old_text&#xff0c;new_text&#xff0c;instance_num) 字段…

Python 爬虫入门(一):从零开始学爬虫 「详细介绍」

Python 爬虫入门&#xff08;一&#xff09;&#xff1a;从零开始学爬虫 「详细介绍」 前言1.爬虫概念1.1 什么是爬虫&#xff1f;1.2 爬虫的工作原理 2. HTTP 简述2.1 什么是 HTTP&#xff1f;2.2 HTTP 请求2.3 HTTP 响应2.4 常见的 HTTP 方法 3. 网页的组成3.1 HTML3.2 CSS3.…