Binary Tree Paths – Leetcode Solution

In this post, we are going to solve the 257. Binary Tree Paths problem of Leetcode. This problem 257. Binary Tree Paths is a Leetcode easy level problem. Let’s see the code, 257. Binary Tree Paths – Leetcode Solution.

Problem

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

Now, let’s see the code of 257. Binary Tree Paths – Leetcode Solution.

Binary Tree Paths – Leetcode Solution

257. Binary Tree Paths – Solution in Java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    
    public void allPaths(TreeNode root, String psf, List<String> paths){
        if(root.left == null && root.right == null){
            paths.add(psf+root.val);
            return;
        }
        if(root.left != null){
            allPaths(root.left,psf+root.val+"->",paths);
        }
        if(root.right != null){
            allPaths(root.right,psf+root.val+"->",paths);
        }
    }
    
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> ans = new ArrayList<>();
        if(root == null) return ans;
        allPaths(root,"",ans);
        return ans;
    }
    
}

257. Binary Tree Paths – Solution in C++

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void binaryTreePaths(vector<string>& result, TreeNode* root, string t) {
        if(!root->left && !root->right) {
            result.push_back(t);
            return;
        }

        if(root->left) binaryTreePaths(result, root->left, t + "->" + to_string(root->left->val));
        if(root->right) binaryTreePaths(result,root->right, t + "->" + to_string(root->right->val));
    }

    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> result;
        if(!root) return result;

        binaryTreePaths(result, root, to_string(root->val));
        return result;
    }
};

257. Binary Tree Paths – Solution in Python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def binaryTreePaths(self, root):
        if not root:
            return []
        res = []
        self.dfs(root, "", res)
        return res
    
    def dfs(self, root, ls, res):
        if not root.left and not root.right:
            res.append(ls+str(root.val))
        if root.left:
            self.dfs(root.left, ls+str(root.val)+"->", res)
        if root.right:
            self.dfs(root.right, ls+str(root.val)+"->", res)
        

Note: This problem 257. Binary Tree Paths is generated by Leetcode but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.

Leave a Comment

Your email address will not be published. Required fields are marked *