872. Trees with similar leaves (recursive, iterative, inorder)

872. Trees with similar leaves

Please consider all the leaves on a binary tree. The values ​​of these leaves are arranged in order from left to right to form a leaf value sequence .

For example, as shown in the figure above, given a tree whose leaf value sequence is (6, 7, 4, 9, 8).

If the leaf value sequences of two binary trees are the same, then we consider them to have similar leaves.

If the given two trees with root nodes root1 and root2 have similar leaves, return true; otherwise, return false.

Example 1:

Solution 1. Recursion

The order of depth-first search is the order of the leaf value sequence of this question.

class Solution {
    List<Integer> res;
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        res = new ArrayList<>();
        dfs(root1);
        dfs(root2);
        return validate();
    }
    public void dfs(TreeNode root){
        if(root.left==null&&root.right==null){
            res.add(root.val);
            return;
        }
        if(root.left != null) dfs(root.left);
        if(root.right != null) dfs(root.right);
    }
    public boolean validate(){
        if(res.size()%2!=0) return false;
        int first = 0, second = res.size()/2;
        for(;second < res.size();++second,++first){
            if(res.get(first)!=res.get(second)) return false;
        }
        return true;
    }
}

Solution 2, iteration

Iteration is a non-recursive version of depth-first search that uses deque to maintain node order.

class Solution {
    public boolean leafSimilar(TreeNode t1, TreeNode t2) {
        List<Integer> l1 = new ArrayList<>(), l2 = new ArrayList<>();
        process(t1, l1);
        process(t2, l2);
        if (l1.size() == l2.size()) {
            for (int i = 0; i < l1.size(); i++) {
                if (!l1.get(i).equals(l2.get(i))) return false;
            }
            return true;
        }
        return false;
    }
    void process(TreeNode root, List<Integer> list) {
        Deque<TreeNode> d = new ArrayDeque<>();
        while (root != null || !d.isEmpty()) {
            while (root != null) {
                d.addLast(root);
                root = root.left;
            }
            root = d.pollLast();
            if (root.left == null && root.right == null) list.add(root.val);
            root = root.right;
        }
    }
}
 

Solution 3: Directly use a checksum to indicate whether they are the same.

I read the comments, tmp=1117. I don’t know why, but I couldn’t pass it after trying several other numbers.

class Solution {
    
    int v1 = 0;
    int v2 = 0;
    int tmp = 1117;
    
    public boolean leafSimilar(TreeNode root1, TreeNode root2) { 
        dfs(root1, 1);
        dfs(root2, 2);
        return v1 == v2;
    }
    public void dfs(TreeNode root,int index){
        if(root.left==null&&root.right==null){
            if(index == 1){
                v1 += tmp;
                v1 ^= root.val;
            }else{
                v2 += tmp;
                v2 ^= root.val;
            }
            
        }
        if(root.left != null) dfs(root.left,index);
        if(root.right != null) dfs(root.right,index);
    }
     
}

Solution three, middle order (double hundred)

There is nothing special about mid-order. The reason for the double hundred is the comparison between arr1 and arr2.

class Solution {
    //ArrayList存储叶子节点
    private ArrayList<Integer> arr1 = new ArrayList<>();
    private ArrayList<Integer> arr2 = new ArrayList<>();
    //中序遍历求从左到右的叶子节点值
    public void midOrderImprovement(TreeNode root, List<Integer> list){
        if(root != null){
            midOrderImprovement(root.left, list);
            if(root.left == null && root.right == null){
                list.add(root.val);
            }
            midOrderImprovement(root.right, list);
        }
    }
    //返回arr1和arr2的比较值
    public boolean leafSimilar(TreeNode root1, TreeNode root2) {
        midOrderImprovement(root1, arr1);
        midOrderImprovement(root2, arr2);
        return arr1.equals(arr2);
    }
}
 

Guess you like

Origin blog.csdn.net/qq_40893490/article/details/116600295