当前位置:网站首页>257. Binary Tree Paths
257. Binary Tree Paths
2022-06-22 12:26:00 【Sterben_Da】
257. Binary Tree Paths
Easy
4139185Add to ListShare
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:
"""
"""
# 遍历
def walkTreePaths(node: TreeNode, s: str):
if node is None:
return
s += "->" + str(node.val)
if node.left is None and node.right is None:
result.append(s)
return
walkTreePaths(node.left, s)
walkTreePaths(node.right, s)
if root.left is None and root.right is None:
return [str(root.val)]
result = []
if root.left is not None:
walkTreePaths(root.left, str(root.val))
if root.right is not None:
walkTreePaths(root.right, str(root.val))
return result边栏推荐
- SAP 系统License查看申请及导入
- Jushan database won two honors of China's information innovation industry in 2022 by AI media consulting
- 基于can总线的A2L文件解析(1)
- Go language notes
- Tianyi cloud explores a new idea of cloud native and edge computing integration
- Heavyweight live | bizdevops: the way to break the technology situation under the tide of digital transformation
- SICF批量激活服务节点
- Sap-mm-migo 311 intra factory transfer inventory
- SAP system cancels user setting ALV global layout
- SiCf batch activation service node
猜你喜欢

In C # development, the third-party components lambdaparser, dynamicexpresso and z.expressions are used to dynamically parse / evaluate string expressions

Sap-abap- how to transfer material master data, supplier master data, work orders, purchase orders and other information to external systems in real time - implicit enhancement.

SAP development keys application SSCR keys application

Ffmpeg converts AMR format to MP3 format

这不会又是一个Go的BUG吧?

CVPR 2022 | visual language model pre training for scene text detection

Hurun Research Institute launched the list of potential enterprises of China's meta universe, and Jushan database was selected as the future star enterprise

Isn't this another go bug?

AcWing第53场周赛

Parallels Desktop 17.1.4pd virtual machine
随机推荐
leetcode 1579. 保证图可完全遍历
MySQL notes
Fluentd is easy to get started. Combined with the rainbow plug-in market, log collection is faster
定金预售的规则思路详解
Sap-abap- how to open a local file
[game] Zhou Yu's skills
A2L file analysis based on CAN bus (1)
SAP 系统License查看申请及导入
leetcode 854. 相似度为 K 的字符串
1961 check if string is a prefix of array
AcWing第53场周赛
On the routing tree of gin
go语言笔记
SAP 系统取消用户设置ALV全局布局
[MySQL] using SQL to find the median
240. Search a 2D Matrix II
Tis tutorial 01- installation
动作捕捉系统用于地下隧道移动机器人定位与建图
How to improve customer conversion rate on the official website
CVPR 2022 | 针对场景文本检测的视觉语言模型预训练