leetcode_树_257. All paths of the binary tree (#)

Hello, everyone, I am a pig who is dominated by cabbage.

A person who loves to study, sleepless and forgets to eat, is obsessed with the girl's chic, calm and indifferent coding handsome boy.

1. Subject content

Given a binary tree, return all paths from the root node to the leaf nodes.
Explanation: A leaf node refers to a node without child nodes.
Example:
Input:
1
/ \
2 3
\
5
Output: ["1->2->5", "1->3"]
Explanation: The path from all root nodes to leaf nodes is: 1->2->5 , 1->3

Two, problem-solving ideas

Use depth-first convenience, set a parameter to save the path. It should be noted that the StringBuilder class is used for efficiency. I also specifically checked the difference between String, StringBuilder, and StringBuffer. In addition, you must create a StringBuilder every time you go in. This is an error-prone place, because you have to make sure to go back to the state of the previous path after going back. If it is not built, then it is added directly on the original basis, which is obviously wrong.

In addition to understanding StringBuilder today, I also know that the front-middle-post-order traversal is based on the depth-first traversal, which means that the front, middle and back are all depth-first traversals. Access starts from the root node, but the difference between them is when do something with root.

Three, code implementation

class Solution {
    
    
    private List<String> res = new LinkedList<>();

    public List<String> binaryTreePaths(TreeNode root) {
    
    
         preOrder(root,"");
         return res;
    }

    public void preOrder(TreeNode root,String path) {
    
    
        if(root == null ) return;
        StringBuilder builder = new StringBuilder(path);
        if(root.left == null && root.right ==null) {
    
    
            builder.append(root.val);
            res.add(builder.toString());
            return;
        }
        builder.append(root.val+"->");
        preOrder(root.left,builder.toString());
        preOrder(root.right,builder.toString());
    }
}

Insert picture description here
After the improvement, here is to extract the public code. I didn't expect the efficiency to be improved so much, so I should pay more attention to the details in this area when typing the code in the future.

class Solution {
    
    

private List<String> res = new ArrayList<>();

    public List<String> binaryTreePaths(TreeNode root) {
    
    
         preOrder(root,"");
         return res;
    }

    public void preOrder(TreeNode root,String path) {
    
    
        if(root == null ) return;
        StringBuilder builder = new StringBuilder(path);
        builder.append(root.val);
        if(root.left == null && root.right ==null) {
    
    
            res.add(builder.toString());
            return;
        }
        builder.append("->");
        preOrder(root.left,builder.toString());
        preOrder(root.right,builder.toString());
    }
}

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_44226263/article/details/109345528