All paths to the binary tree
Give you a binary tree root node root, in any order, returning all paths from the root node to the leaf node.
A leaf node refers to a node without child nodes.
- Example 1:
Input: root = [1,2,3,null,5]
Output: ["1->2->5","1->3"]
- Example 2:
Input: root = [1]
Output: ["1"]
hint:
The number of nodes in the tree is within range [1, 100]
-100 <= <= 100
Method 1: Depth-first traversal search (Java)
The most intuitive way is to use depth-first search. When traversing the binary tree with depth-first search, we need to consider the current node and its child nodes.
If the current node is not a leaf node, add the node at the end of the current path and continue to recursively traverse each child node of the node.
If the current node is a leaf node, after adding the node at the end of the current path, we get a path from the root node to the leaf node, and add the path to the answer.
Recursion two-step:
(1) Find duplicate subproblems.
- The order of pre-order traversal is: root node, left subtree, and right subtree.
- In this question, the same order is: add the root node to the path, recursively the left subtree, and recursively the right subtree.
- The same operation is also true for the left and right subtrees.
(2) Determine the termination conditions.
For each path of all paths of the binary tree, the end of the current path when traversing to the leaf node. And add the current path to the result set.
class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> paths = new ArrayList<String>(); constructPaths(root, "", paths); return paths; } public void constructPaths(TreeNode root, String path, List<String> paths) { if (root != null) { StringBuffer pathSB = new StringBuffer(path); (()); if ( == null && == null) { // The current node is a leaf node (()); // Add the path to the answer } else { ("->"); // The current node is not a leaf node, continue to recursively traverse constructPaths(, (), paths); constructPaths(, (), paths); } } } }
Time complexity: O(N^2)
Space complexity: O(N^2)
Method 2: Breadth priority traversal (Go)
We can also use breadth-first search to achieve it.
- We maintain a queue, store nodes, and the path to the node. At the beginning, there was only a root node in this queue.
- In each step of iteration, we take out the first node in the queue
- If it is a leaf node, add its corresponding path to the answer. If it is not a leaf node, all its child nodes are added to the end of the queue.
- Bread priority search ends when queue is empty
func binaryTreePaths(root *TreeNode) []string { paths := []string{} if root == nil { return paths } nodeQueue := []*TreeNode{} pathQueue := []string{} nodeQueue = append(nodeQueue, root) pathQueue = append(pathQueue, ()) for i := 0; i < len(nodeQueue); i++ { node, path := nodeQueue[i], pathQueue[i] if == nil && == nil { paths = append(paths, path) continue } if != nil { nodeQueue = append(nodeQueue, ) pathQueue = append(pathQueue, path + "->" + ()) } if != nil { nodeQueue = append(nodeQueue, ) pathQueue = append(pathQueue, path + "->" + ()) } } return paths }
Time complexity: O(N^2)
Space complexity: O(N^2)
The above is a detailed explanation of all path examples of the Go Java algorithm binary tree. For more information about all paths of the Go Java algorithm binary tree, please pay attention to my other related articles!