Binary Tree Paths
Function Name: binaryTreePaths
Description
Write a function that returns all the paths from the root to leaf nodes in a binary tree. A path is defined as a sequence of nodes starting from the root node and ending at any leaf node. The function should output the paths as an array of string representations, where each string contains the path's nodes' values separated by '->'. Your function should work with any binary tree, not necessarily a binary search tree.
Requirements
- The binary tree node structure is given by 'TreeNode', each having an integer value and two children, 'left' and 'right'.
- If the tree is empty, return an empty array.
- Think of an efficient way to traverse the tree and gather the paths.
- The function should handle binary trees with duplicate values.
- Use depth-first search to explore the paths.
- The output should be an array of strings sorted in ascending order if the inputs were numerical.
Examples
Given the following binary tree: 1 / \ 2 3 \ 5binaryTreePaths(root) should return ['1->2->5', '1->3'].For a tree with only one node with value 1, binaryTreePaths(root) should return ['1'].
Links
Substring with Concatenation of All Words
Sun Oct 13 2024
Write a function `findSubstring(s, words)` that takes a string `s` and an array of strings `words` as parameters.All strings in `words` are of the same length.The function should return all starting indices in `s` where a concatenation of each word in `words` exactly once forms a substring.The indices should be returned in ascending order.The function should handle overlapping words and words can appear in any order.
Balanced Brackets
Tue Oct 15 2024
Write a function named 'isBalanced' that takes a string containing only bracket characters: '(', ')', '{', '}', '[' and ']', and determines if the input string is balanced. A string is considered balanced if brackets are closed in the correct order and every opening bracket has a corresponding closing bracket.The function should return true if the input string is balanced, otherwise return false.