Arena / Problems /

257. Binary Tree Paths

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #257

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.

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
⚔️

Vacant Python Throne

No 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.

Contenders & Community Submissions (2)

Peer solutions & challenges
#1 'path' as a parameter by Anonymous Developer
0
# 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
#2 Use one 'path' by Anonymous Developer
0
# 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
⚔️

Vacant Java Throne

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.

⚔️

Vacant C++ Throne

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.

⚔️

Vacant Javascript Throne

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.

⚔️

Vacant Go Throne

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.

⚔️

Vacant Rust Throne

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.

⚔️

Vacant C# Throne

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.

⚔️

Vacant Ruby Throne

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.

⚔️

Vacant Swift Throne

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.

⚔️

Vacant Kotlin Throne

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.