檢查兩個二叉樹的所有級別是否為字謎


難度級別
經常問 土磚 亞馬遜 Facebook 狂徒 風箏 灰色橙色
字謎 二叉樹 隊列

問題陳述

問題“檢查兩個二叉樹的所有級別是否為字謎”表示您獲得了兩棵二叉樹,請檢查兩個樹的所有級別是否為字謎。

範例檔案

輸入

檢查兩個二叉樹的所有級別是否為字謎

true

輸入

檢查兩個二叉樹的所有級別是否為字謎

false

檢查兩個二叉樹的所有級別是否為字謎的算法

我們將使用 散列 解決這個問題。 遍歷兩個級別 同時地。 對於第一棵樹,將當前水平的元素和頻率存儲在 哈希圖 對於第二棵樹的當前級別,如果HashMap中不存在當前元素。 所有級別都不是字謎。 否則,減少該元素在HashMap中的出現頻率。 如果在遍歷結束時HashMap為空,則兩棵樹的該層均以anagram繼續到下一層,否則所有層都不為 字謎.

  1. 創建兩個 尾巴 q1和q2,q1用於遍歷樹1,q2用於遍歷q2。
  2. 將樹1的根推到q1,將樹2的根推到q2。
  3. 當q1不為空或q2不為空時,重複步驟4、5和6。
  4. 創建一個HashMap來存儲元素和當前關卡元素的頻率。 初始化一個整數size1作為q1的大小。 運行從0到小於size1的循環。 在每次迭代時,從隊列q1中彈出一個元素,並將其添加到HashMap中。 將當前元素的子元素推入 隊列.
  5. 初始化一個整數size2作為q2的大小。 運行從0到小於size2的循環。 每次迭代時,從隊列q2中彈出一個元素,如果該元素存在於HashMap中,則將其頻率降低1,否則立即返回false。
  6. 如果在循環結束時,HashMap包含一個元素,則返回false,否則,這兩個樹的級別均為字謎,繼續進行下一個級別。
  7. 如果到達此處,則兩棵樹的所有層都是字謎,因此返回true。

推薦碼

用於檢查兩個二叉樹的所有級別是否為字謎的Java代碼

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Queue;

class CheckIfAllLevelsOfTwoBinaryTreeAreAnagramsOrNot {
    // class representing node of a binary tree
    static class Node {
        int data;
        Node left, right;

        public Node(int data) {
            this.data = data;
        }
    }

    private static boolean checkIsAnagrams(Node tree1, Node tree2) {
        // create two queues
        Queue<Node> q1 = new LinkedList<>();
        Queue<Node> q2 = new LinkedList<>();
        // add root of tree1 to q1
        q1.add(tree1);
        // add root of tree2 to q2
        q2.add(tree2);

        // while either of q1 or q2 is not empty
        while (!q1.isEmpty() || !q2.isEmpty()) {
            // create a hash map to store freq of elements of a level
            HashMap<Integer, Integer> freq = new HashMap<>();

            // traverse this level of tree1
            int size1 = q1.size();
            for (int i = 0; i < size1; i++) {
                // remove a node from queue
                Node curr = q1.poll();
                // add the element to hash map
                if (freq.containsKey(curr.data)) {
                    freq.put(curr.data, freq.get(curr.data) + 1);
                } else {
                    freq.put(curr.data, 1);
                }

                // add curr's children to queue
                if (curr.left != null)
                    q1.add(curr.left);
                if (curr.right != null)
                    q1.add(curr.right);
            }

            // traverse this level of tree2
            int size2 = q2.size();
            for (int i = 0; i < size2; i++) {
                // remove a node from q2
                Node curr = q2.poll();
                // decrease the frequency of this element in hash map
                if (freq.containsKey(curr.data)) {
                    int frequency = freq.get(curr.data);
                    frequency--;
                    if (frequency == 0) {
                        freq.remove(curr.data);
                    } else {
                        freq.put(curr.data, frequency);
                    }
                } else {
                    return false;
                }

                // add curr's children to queue
                if (curr.left != null)
                    q2.add(curr.left);
                if (curr.right != null)
                    q2.add(curr.right);
            }

            // if there is an element in the hash map
            // the two tree's current levels are not anagrams
            if (freq.size() > 0) {
                return false;
            }
        }

        // all the levels are anagrams, return true
        return true;
    }

    public static void main(String[] args) {
        // Example 1
        Node tree1_1 = new Node(5);
        tree1_1.left = new Node(4);
        tree1_1.right = new Node(3);
        tree1_1.left.left = new Node(2);
        tree1_1.left.right = new Node(1);

        Node tree2_1 = new Node(5);
        tree2_1.left = new Node(3);
        tree2_1.right = new Node(4);
        tree2_1.left.left = new Node(1);
        tree2_1.right.left = new Node(2);

        System.out.println(checkIsAnagrams(tree1_1, tree2_1));

        // Example 2
        Node tree1_2 = new Node(5);
        tree1_2.left = new Node(7);
        tree1_2.right = new Node(8);
        tree1_2.left.left = new Node(9);

        Node tree2_2 = new Node(5);
        tree2_2.left = new Node(7);
        tree2_2.right = new Node(8);
        tree2_2.left.left = new Node(1);
        tree2_2.right.left = new Node(2);

        System.out.println(checkIsAnagrams(tree1_2, tree2_2));
    }
}
true
false

用於檢查兩個二叉樹的所有級別是否為字謎的C ++代碼

#include<bits/stdc++.h> 
using namespace std; 

// class representing node of a binary tree
class Node {
    public:
    int data;
    Node *left;
    Node *right;
    
    Node(int d) {
        data = d;
        left = right = NULL;
    }
};

// function to create a new node with given data
Node* newNode(int data) {
    Node *node = new Node(data);
    return node;
}

bool checkIsAnagrams(Node *tree1, Node *tree2) {
    // create two queues
    queue<Node *> q1;
    queue<Node *> q2;
    // add root of tree1 to q1
    q1.push(tree1);
    // add root of tree2 to q2
    q2.push(tree2);
    
    // while either of q1 or q2 is not empty
    while (!q1.empty() || !q2.empty()) {
        // create a hash map to store freq of elements of a level
        unordered_map<int, int> freq;
        
        // traverse this level of tree1
        int size1 = q1.size();
        for (int i = 0; i < size1; i++) {
            // remove a node from queue
            Node *curr = q1.front();
            q1.pop();
            
            // add the element to hash map
            auto itr = freq.find(curr->data);
            if (itr != freq.end()) {
                itr->second++;
            } else {
                freq.insert(make_pair(curr->data, 1));
            }
            
            // add curr's children to queue
            if (curr->left != NULL)
                q1.push(curr->left);
            if (curr->right != NULL)
                q1.push(curr->right);
        }
        
        // traverse this level of tree2
        int size2 = q2.size();
        for (int i = 0; i < size2; i++) {
            // remove a node from q2
            Node *curr = q2.front();
            q2.pop();
    
            // decrease the frequency of this element in hash map
            auto itr = freq.find(curr->data);
            if (itr != freq.end()) {
                itr->second--;
                if (itr->second == 0) {
                    freq.erase(itr);
                }
            } else {
                return false;
            }
            
            // add curr's children to queue
            if (curr->left != NULL)
                q2.push(curr->left);
            if (curr->right != NULL)
                q2.push(curr->right);
        }
        
        // if there is an element in the hash map
        // the two tree's current levels are not anagrams
        if (freq.size() != 0)
            return false;
    }
    
    // all the levels are anagrams, return true
    return true;
}

int main() {
    // Example 1
    Node *tree1_1 = newNode(5);
    tree1_1->left = newNode(4);
    tree1_1->right = newNode(3);
    tree1_1->left->left = newNode(2);
    tree1_1->left->right = newNode(1);

    Node *tree2_1 = new Node(5);
    tree2_1->left = newNode(3);
    tree2_1->right = newNode(4);
    tree2_1->left->left = newNode(1);
    tree2_1->right->left = newNode(2);

    if (checkIsAnagrams(tree1_1, tree2_1)) {
        cout<<"true"<<endl;
    } else {
        cout<<"false"<<endl;
    }

    // Example 2
    Node *tree1_2 = newNode(5);
    tree1_2->left = newNode(7);
    tree1_2->right = newNode(8);
    tree1_2->left->left = newNode(9);

    Node *tree2_2 = newNode(5);
    tree2_2->left = newNode(7);
    tree2_2->right = newNode(8);
    tree2_2->left->left = newNode(1);
    tree2_2->right->left = newNode(2);

    if (checkIsAnagrams(tree1_2, tree2_2)) {
        cout<<"true"<<endl;
    } else {
        cout<<"false"<<endl;
    }   
    
    return 0;
}
true
false

複雜度分析

當我們只遍歷兩棵樹一次並使用兩個隊列進行級別順序遍歷時,

時間複雜度 = O(n +米)
空間複雜度 = O(n +米)
其中n是樹1中的節點數,m是樹2中的節點數。