Binary tree OJ problem (on)

✅Daily exercise: 100. The same tree - LeetCode


     The meaning of the title is that not only the structure of the two trees must be the same, but also the value of each node must be the same. If the above two conditions are met, then it is established!

Problem-solving ideas:

Consider from three aspects:

1. If both p and q are empty, they must be the same;

2. If p is empty and q is not empty, or if p is not empty and q is empty, then it must be different;

3. If both are not empty, then the root node needs to be judged. If the root nodes are not the same, then they must be different. If they are the same, we need to compare the values ​​of the left and right subtrees with the structure of the left and right subtrees;

code:

public boolean isSameTree(TreeNode p, TreeNode q) {
        //如果p,q都为空,那么这2个树一定相同
        if (p == null && q == null) {
            return true;
        }
        //如果q为空,p不为空,那么一定不相同,或者p为空,q不为空,那么一定不相同
        if (p != null && q == null||p == null && q != null) {
            return false;
        }
        //如果p,q都不为空,那么要判断值,如果值不相同,那么一定不相同
        if (p.val != q.val) {
            return false;
        }
        //如果p,q都不为空,并且p,q的值相同,那么要判断p,q的左右子树的值,如果相同为真,反之;
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }

✅Daily practice: 572. Subtree of another tree - LeetCode


 Problem-solving ideas:

1. If the root node is empty, return false;

2. If the root nodes are the same, then we need to judge whether the two trees are the same, we can use the isSameTree method written above to judge, if they are the same, then subRoot is the subtree of root;

3. If the root nodes are not the same, we need to find whether there is a tree with the same subRoot in the left subtree or right subtree;

code:

public boolean isSubtree(TreeNode root, TreeNode subRoot) {
        if(root == null||subRoot == null){
            return false;
        }
        //判断2棵树是否相同
        if(isSameTree(root,subRoot)){
            return true;
        }
        //判断左子树是否有subRoot
        if(isSubtree(root.left,subRoot)){
            return true;
        }
        //判断右子树是否有subRoot
        if(isSubtree(root.right,subRoot)){
            return true;
        }
        return false;
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        //如果p,q都为空,那么这2个树一定相同
        if (p == null && q == null) {
            return true;
        }
        //如果q为空,p不为空,那么一定不相同
        if (p != null && q == null) {
            return false;
        }
        //如果p为空,q不为空,那么一定不相同
        if (p == null && q != null) {
            return false;
        }
        //如果p,q都不为空,那么要判断值,如果值不相同,那么一定不相同
        if (p.val != q.val) {
            return false;
        }
        //如果p,q都不为空,并且p,q的值相同,那么要判断p,q的左右子树的值,如果相同为真,反之;
        return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
    }

✅Daily Practice: 226. Flip Binary Tree - LeetCode


 

Problem-solving ideas:

Using the idea of ​​recursion, if the root node is not empty, recursively exchange the values ​​of the left and right subtrees;

public TreeNode invertTree(TreeNode root) {
        if (root == null) {
            return null;
        }
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }

✅Daily exercise: 110. Balance a binary tree - LeetCode


 

 Problem-solving ideas:

The meaning of the title is that the absolute value of the height difference between the left and right subtrees of each node cannot exceed 1, that is, a balanced binary tree, which meets the needs of the title;

code:

public boolean isBalanced(TreeNode root) {
       if(root == null){
           return true;
       }
       int leftH = getHeight(root.left);
       int rightH =getHeight(root.right);
       return Math.abs(leftH-rightH)<=1 && isBalanced(root.left) && isBalanced(root.right);
    
    }
    public int getHeight(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int leftHeight = getHeight(root.left);
        int rightHeight = getHeight(root.right);
        return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
    }

✅Daily practice: 101. Symmetric binary tree - LeetCode

 

Problem-solving ideas:

1. If the root node is empty, it is symmetrical;

2. If the root node is not empty, the left subtree of the root node is empty, the right subtree of the root node is not empty, or the left subtree of the root node is not empty, and the right subtree of the root node is empty, then must asymmetrical;

3. If the value of the left subtree of the root node is not equal to the value of the right subtree, then it must be asymmetrical;

4. If none of the above conditions are met, then we need to judge whether the left subtree of the left subtree is equal to the right subtree of the right subtree, and whether the right subtree of the left subtree is equal to the left subtree of the right subtree. Symmetric if equal;

code:

public boolean isSymmetric(TreeNode root) {
        if (root == null) {
            return true;
        }
        return isSymmetricChild(root.left, root.right);

    }

    public boolean isSymmetricChild(TreeNode leftTree, TreeNode rightTree) {
        if (leftTree == null && rightTree == null) {
            return true;
        }
        if (leftTree != null && rightTree == null || leftTree == null && rightTree != null) {
            return false;
        }
        if (leftTree.val != rightTree.val) {
            return false;
        }
        return isSymmetricChild(leftTree.left, rightTree.right) && isSymmetricChild(leftTree.right, rightTree.left);
    }

✅Daily exercise: 102. Level order traversal of binary tree - LeetCode


 

Problem-solving ideas:

Hierarchical traversal is traversal from top to bottom and from left to right. Hierarchical traversal is simpler than other traversals, but the code is not so simple to implement. We can use queues to implement it, and use the characteristics of queues to achieve first-in-first-out :

First judge the root node, if it is empty, return, if it is not empty, put the root node into the queue, use the while loop to judge whether the current queue is empty, then define a variable to store the value of the root node, and then print, print After that, judge the left and right subtrees of the root node. If they are not empty, they will join the queue, and we can get the hierarchical order traversal by executing them sequentially. The code:

public void levelOrder1(TreeNode root) {
        if (root == null) {
            return;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode cur = queue.poll();
            System.out.print(cur.val + " ");
            if (cur.left != null) {
                queue.offer(cur.left);
            }
            if (cur.right != null) {
                queue.offer(cur.right);
            }
        }
    }

This is a method without a return value, but the return value of this oj question is List<List<Integer>>, so we have to write this question in a different way. The general idea is that we need a loop of 2 , use whether the current queue is empty as the first loop condition, and the number of elements in the current queue as the sub-loop condition, code:

public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> list = new ArrayList<>();
        if (root == null) {
            return list;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {

            int size = queue.size();//用来判断当前队列的大小
            //System.out.print(cur.val);
            List<Integer> tmp = new ArrayList<>();
            while (size != 0) {
                TreeNode cur = queue.poll();
                tmp.add(cur.val);
                size--;
                if (cur.left != null) {
                    queue.offer(cur.left);
                }
                if (cur.right != null) {
                    queue.offer(cur.right);
                }
            }
            list.add(tmp);//将每层的结果放到list中
        }
        return list;
    }

Guess you like

Origin blog.csdn.net/m0_63635730/article/details/128976646