[LeetCode-Java Exercise] 100. The same tree (simple)

1. Title description

Insert picture description here

2. Problem solving ideas

Method 1: Depth-first search
If both binary trees are empty, the two binary trees are the same. If one and only one of the two binary trees is empty, the two binary trees must be different.
If the two binary trees are not empty, first determine whether the values ​​of their root nodes are the same. If they are not the same, the two binary trees must be different. If they are the same, then determine whether the left subtree and the right subtree of the two binary trees are the same. Are they the same. This is a recursive process, so depth-first search can be used to recursively determine whether two binary trees are the same.

Method 2: Breadth-first search The breadth-first search
can also be used to determine whether two binary trees are the same. Similarly, first determine whether the two binary trees are empty. If the two binary trees are not empty, the breadth-first search is started from the root nodes of the two binary trees.
Two queues are used to store the nodes of two binary trees respectively. Initially, the root nodes of the two binary trees are added to two queues respectively. Each time a node is taken from the two queues, the following comparison operations are performed.
Compare the values ​​of two nodes. If the values ​​of the two nodes are not the same, the two binary trees must be different;
if the values ​​of the two nodes are the same, judge whether the child nodes of the two nodes are empty, if there is only the left child node of one node Is empty, or only the right child node of one node is empty, then the structure of the two binary trees is different, so the two binary trees must be different;
if the structure of the child nodes of the two nodes is the same, the non-empty child nodes of the two nodes are separated When joining two queues, you need to pay attention to the order when adding child nodes to the queue. If the left and right child nodes are not empty, add the left child node first, and then add the right child node.
If the two queues are empty at the end of the search, the two binary trees are the same. If only one queue is empty, the structure of the two binary trees is different, so the two binary trees are different.

3. Code implementation

method one:

class Solution {
    
    
    public boolean isSameTree(TreeNode p, TreeNode q) {
    
    
        if (p == null && q == null) {
    
    
            return true;
        } else if (p == null || q == null) {
    
    
            return false;
        } else if (p.val != q.val) {
    
    
            return false;
        } else {
    
    
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }
    }
}

Method Two:

class Solution {
    
    
    public boolean isSameTree(TreeNode p, TreeNode q) {
    
    
        if (p == null && q == null) {
    
    
            return true;
        } else if (p == null || q == null) {
    
    
            return false;
        }
        Queue<TreeNode> queue1 = new LinkedList<TreeNode>();
        Queue<TreeNode> queue2 = new LinkedList<TreeNode>();
        queue1.offer(p);
        queue2.offer(q);
        while (!queue1.isEmpty() && !queue2.isEmpty()) {
    
    
            TreeNode node1 = queue1.poll();
            TreeNode node2 = queue2.poll();
            if (node1.val != node2.val) {
    
    
                return false;
            }
            TreeNode left1 = node1.left, right1 = node1.right, left2 = node2.left, right2 = node2.right;
            if (left1 == null ^ left2 == null) {
    
    
                return false;
            }
            if (right1 == null ^ right2 == null) {
    
    
                return false;
            }
            if (left1 != null) {
    
    
                queue1.offer(left1);
            }
            if (right1 != null) {
    
    
                queue1.offer(right1);
            }
            if (left2 != null) {
    
    
                queue2.offer(left2);
            }
            if (right2 != null) {
    
    
                queue2.offer(right2);
            }
        }
        return queue1.isEmpty() && queue2.isEmpty();
    }
}

Guess you like

Origin blog.csdn.net/weixin_48683410/article/details/114223522
Recommended