[LeetCode-150 classic interview questions-day15]

Table of contents

104.The maximum depth of a binary tree

100.The same tree 

 226. Flip a binary tree

 101. Symmetric binary tree

 105. Construct a binary tree from preorder and inorder traversal sequences

 106. Construct a binary tree from inorder and postorder traversal sequences

 117. Fill the next right node pointer of each node Ⅱ


 

104.The maximum depth of a binary tree

Question meaning:

Given a binary tree  root , return its maximum depth.

The maximum depth of a binary tree   is the number of nodes on the longest path from the root node to the furthest leaf node.

【Input sample】

root=[3,9,20,null,null,15,7]

【Output sample】

3

Problem-solving ideas: recursion

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        //1是当树的根节点不为空时,加上根
        return 1 + Math.max(maxDepth(root.right),maxDepth(root.left));
    }
}

Time: Beat 100.00%

Memory: Beaten by 36.81%

100.The same tree 

Question meaning:

Given the sum of the root nodes of two binary trees  p ,  q write a function to test whether the two trees are the same.

Two trees are considered identical if they are structurally identical and have nodes with the same values.

【Input sample】

p=[1,2,3], q=[1,2,3]

【Output sample】

true

Problem-solving ideas: recursion

1. First determine whether the value of the current root node is the same

2. Then determine whether both have left subtrees and right subtrees

3. Recursively determine the left subtree and right subtree

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p == null && q == null){
            return true;
        }
        //如果说两者都会null,会在上面的分支语句返回true
        //这里判断的是只有一方为null的情况下
        if(p == null || q == null){
            return false;
        }
        //根都不为null,判断值是否相同
        if(p.val != q.val){
            return false;
        }
        return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }
}

Time: Beat 100.00%

Memory: Beaten by 41.80%

 226. Flip a binary tree

Question meaning:

You are given the root node of a binary tree  root , flip the binary tree and return its root node.

 

【Input sample】

root = [4,2,7,1,3,6,9]

【Output sample】

[4,7,2,9,6,3,1]

Problem-solving ideas: recursion

1. Continuously exchange the left and right subtrees of the current node and implement it recursively

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root == null){
            return root;
        }
        //左右子树交换
        TreeNode temp = root.right;
        root.right = root.left;
        root.left =  temp;
        //交换左子树
        invertTree(root.left);
        //交换右子树
        invertTree(root.right);
        return root;
    }
}

Time: Beat 100.00%

Memory: Beat 88.10%

 101. Symmetric binary tree

Question meaning:

Given the root node of a binary tree  root , check whether it is axially symmetric.

 

【Input sample】

root = [1,2,2,3,4,4,3]

【Output sample】

true

Problem-solving ideas: recursion

1. The recursive function determines whether the left subtree and right subtree of the node are symmetrical; by splitting the left subtree and the right subtree, the problem changes to determining whether the same tree is the same.

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null){
            return true;
        }
        return cmp(root.left, root.right);
    }
    public boolean cmp(TreeNode root1, TreeNode root2){
        if(root1 == null && root2 == null){
            return true;
        }
        if(root1 == null || root2 == null || root1.val != root2.val){
            return false;
        }
        return cmp(root1.left,root2.right) && cmp(root1.right,root2.left);

    }
}

Time: Beat 100.00%

Memory: Beaten by 82.85%

 105. Construct a binary tree from preorder and inorder traversal sequences

Question meaning:

Given two integer arrays  preorder and  inorder , where  is a pre-order traversalpreorder  of a binary tree and  where is an in-order traversal  of the same tree , construct a binary tree and return its root node.inorder

 

【Input sample】

preorder = [3,9,20,15,7], inorder = [9,3,15,20,7]

【Output sample】

[3,9,20,null,null,15,7]

Problem-solving ideas:

1. The process of pre-order traversal is: root left and right; the process of in-order traversal is: left root right.

2. According to the rules, the first thing you need to find is the root node. The left subtree of the root in the inorder array is the left subtree, and the right subtree of the root is the right subtree;

3. Then construct the left subtree and right subtree respectively;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    private Map<Integer,Integer> indexMap;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        int n = preorder.length;//一共n个节点
        //构造哈希映射,快速定位根节点
        indexMap = new HashMap<Integer,Integer>();
        for(int i=0;i<n;i++){
            indexMap.put(inorder[i],i);
        }
        return myBuildTree(preorder,inorder,0,n-1,0,n-1);
    }

   public TreeNode myBuildTree(int[] preorder, int[] inorder, int preorder_left, int preorder_right, int inorder_left, int inorder_right) {
        if (preorder_left > preorder_right) {
            return null;
        }
        //前序遍历找到根节点
        int preorder_root = preorder_left;
        //中序遍历定位根节点
        int inorder_root = indexMap.get( preorder[preorder_root]);

        //建立根节点
        TreeNode root = new TreeNode(preorder[preorder_root]);
        //确定左子树节点数目
        int size_left_subtree = inorder_root - inorder_left;
        //递归构造左子树,连接到根节点
        root.left = myBuildTree(preorder,inorder,preorder_left+1,preorder_left+size_left_subtree,
        inorder_left, inorder_root-1);

        //递归构造右子树
        root.right = myBuildTree(preorder,inorder,preorder_left+size_left_subtree+1, preorder_right,
        inorder_root+1, inorder_right);
        return root;
   }
}

Time: Beat 99.18%

Memory: Beaten by 23.53%

 106. Construct a binary tree from inorder and postorder traversal sequences

Question meaning:

Given two integer arrays  inorder and  postorder , where  inorder is an in-order traversal of a binary tree and  postorder is a post-order traversal of the same tree, please construct and return this  binary tree  .

【Input sample】

inorder = [9,3,15,20,7],mail order = [9,15,7,20,3]

【Output sample】

[3,9,20,null,null,15,7]

Problem-solving ideas:

1. The process of in-order traversal is: left root and right; the process of post-order traversal is: left and right roots;.

2. According to the rules, the first thing you need to find is the root node. The left subtree of the root in the inorder array is the left subtree, and the right subtree of the root is the right subtree;

3. Then construct the left subtree and right subtree respectively;

class Solution {
    private Map<Integer,Integer> indexMap;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        int n = postorder.length;//一共n个节点
        //构造哈希映射,快速定位根节点
        indexMap = new HashMap<Integer,Integer>();
        for(int i=0;i<n;i++){
            indexMap.put(inorder[i],i);
        }
        return myBuildTree(inorder,postorder,0,n-1,0,n-1);
    }

   public TreeNode myBuildTree(int[] inorder,int[] postorder, int inorder_left, int inorder_right,int  postorder_left, int postorder_right) {
        if (postorder_left > postorder_right || inorder_left > inorder_right) {
            return null;
        }
        //后序遍历找到根节点
        int postorder_root = postorder[postorder_right];
        //中序遍历定位根节点
        int inorder_root = indexMap.get(postorder_root);

        //建立根节点
        TreeNode root = new TreeNode(postorder_root);
        //确定左子树节点数目
        int size_left_subtree = inorder_root - inorder_left;
        //递归构造左子树,连接到根节点
        root.left = myBuildTree(inorder, postorder, inorder_left, inorder_root-1,
        postorder_left,postorder_left+size_left_subtree-1);

        //递归构造右子树
        root.right = myBuildTree(inorder,postorder, inorder_root+1, inorder_right,
        postorder_left+size_left_subtree,postorder_right-1);
        return root;
   }
}

Time: Beat 99.21%

Memory: Beaten by 61.89%

 117. Fill the next right node pointer of each node Ⅱ

Question meaning:

Given a binary tree:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

Fill each of its next pointers so that this pointer points to its next right node. If the next right node is not found, the next pointer is set to  NULL .

Initially, all next pointers are set to  NULL .

 

【Input sample】

root=[1,2,3,4,5,null,7]

【Output sample】

[1,#,2,3,#,4,5,7,#]

Problem-solving ideas:

Use breadth-first search to complete this question

/*
// Definition for a Node.
class Node {
    public int val;
    public Node left;
    public Node right;
    public Node next;

    public Node() {}
    
    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, Node _left, Node _right, Node _next) {
        val = _val;
        left = _left;
        right = _right;
        next = _next;
    }
};
*/

class Solution {
    public Node connect(Node root) {
        if(root== null){
            return root;
        }
        //队列存储节点信息
        Queue<Node> queue = new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty()){
            //每一层的数量
            int levelCount = queue.size();
            //前一个节点
            Node pre = null;
            for(int i=0;i<levelCount;++i){
                //出队
                Node node = queue.poll();
                if(pre != null){
                    //不是第一个节点
                    pre.next = node;
                }
                pre = node;
                //查看左右节点是否为空,不空入队
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right);
                }
            }
        }
        return root;
    }
}

Time: Beat 76.40%

Memory: Beaten by 5.16%

Guess you like

Origin blog.csdn.net/qq_37998848/article/details/132507776