You are given the root of a binary tree.
Return all root-to-leaf paths in any order.
A leaf is a node with no children.
[1, 100].-100 <= Node.val <= 100No developer has claimed the Python throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
# 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: TreeNode | None) -> list[str]:
self.paths = []
self.traverse(root, [])
return self.paths
def traverse(self, node, path):
if node is None:
return
path = path + [node.val]
if is_leaf(node):
self.paths.append('->'.join([str(item) for item in path]))
return
self.traverse(node.left, path)
self.traverse(node.right, path)
def is_leaf(node):
return node.left is None and node.right is None
# 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: TreeNode | None) -> list[str]:
self.paths = []
self.path = [root.val]
self.traverse(root)
return self.paths
def traverse(self, node):
if is_leaf(node):
self.paths.append('->'.join([str(item) for item in self.path]))
return
if node.left:
self.path.append(node.left.val)
self.traverse(node.left)
self.path.pop()
if node.right:
self.path.append(node.right.val)
self.traverse(node.right)
self.path.pop()
def is_leaf(node):
return node.left is None and node.right is None
No developer has claimed the Java throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C++ throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Javascript throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Go throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Rust throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the C# throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Ruby throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Swift throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.
No developer has claimed the Kotlin throne for LeetCode #257 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.