[Daily practice] Google interview question: Flip binary search tree with JAVA

foreword

Many students should be able to simulate a binary tree, so how many students can write a flipped binary tree? There's a google interview question: 90% of our engineers use the software you wrote (Homebrew), but you can't write flipping a binary tree question on the whiteboard during the interview, it's too bad.
insert image description here

topic

Flip a binary tree
Example:
Enter:

     4
   /   \
  2     7
 / \   / \
1   3 6   9

output:

     4
   /   \
  7     2
 / \   / \
9   6 3   1

analyze

1. Flipping the binary tree according to the example is to exchange all the left and right nodes.
2. We can get each node, and then exchange the left and right nodes. Of course, this is a recursive operation.
3. To facilitate our testing, we use binary search Tree demo.
4. Binary search tree rules: the left child node must be smaller than the parent node, the right child node must be greater than the parent node, the maximum value of all left child nodes is not greater than the current parent node, and the minimum value of all right child nodes is not less than the current parent node.

Combat demonstration

For the convenience of testing, we need to create an algorithm to build a binary tree first, and then write an algorithm to flip the binary tree

1. Create a search binary tree

For the convenience of demonstration, we only implement the algorithm of adding nodes

/**
 * 创建二叉搜索树
 * 定义 左节点小于父节点,右节点大于父节点
 *
 */
private void createTree(Node root,int val){
    
    
    if(Objects.isNull(root) || val == 0){
    
    
        return;
    }
    //封装节点
    Node node = new Node(val);
    //节点放入
    setNode(root,node);
}



/**
 * 设置节点
 * @param root
 * @param node
 * @author senfel
 * @date 2023/5/4 10:03
 * @return void
 */
private void setNode(Node root, Node node) {
    
    
   if(node.val > root.val){
    
    
       //右节点
       if(root.rightNode == null){
    
    
           root.rightNode = node;
       }else{
    
    
           setNode(root.rightNode,node);
       }
   }else if(node.val < root.val){
    
    
       //左节点
       if(root.leftNode == null){
    
    
           root.leftNode = node;
       }else{
    
    
           setNode(root.leftNode,node);
       }
   }else{
    
    
       //相等不做处理
       return;
   }
}

/**
 * 节点
 */
static class Node{
    
    
    /**
     * 值
     */
    int val;
    /**
     * 左节点
     */
    Node leftNode;
    /**
     * 右节点
     */
    Node rightNode;

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

2. Inorder traversal of a binary search tree

In order to facilitate the display, we use the in-order traversal of the binary tree

/**
 * 遍历打印二叉搜索树
 * 中序打印
 * @param node
 * @author senfel
 * @date 2023/5/4 10:28
 * @return void
 */
public void toString(Node node){
    
    
    if(Objects.isNull(node)){
    
    
        return;
    }
    toString(node.leftNode);
    System.err.println("node-val:"+node.val);
    toString(node.rightNode);
}

3. Create a binary search tree according to the meaning of the question and display it

3.1 Create a binary search tree test case

public static void main(String[] args) {
    
    
    Node node = new Node(4);
    ReverseTreeDemo reverTreeDemo = new ReverseTreeDemo();
    reverTreeDemo.createTree(node,2);
    reverTreeDemo.createTree(node,7);
    reverTreeDemo.createTree(node,1);
    reverTreeDemo.createTree(node,3);
    reverTreeDemo.createTree(node,6);
    reverTreeDemo.createTree(node,9);
    System.err.println(node);
    reverTreeDemo.toString(node);
}

3.2 View test results

insert image description here

3.3 Print binary tree results in inorder

node-val:1
node-val:2
node-val:3
node-val:4
node-val:6
node-val:7
node-val:9


     4
   /   \
  2     7
 / \   / \
1   3 6   9

4. The algorithm adds a binary tree flip method

/**
 * 翻转节点
 * 为了符合题意,相当于左右子树交换位置
 * @param root
 * @author senfel
 * @date 2023/5/4 10:37
 * @return void
 */
private void reverseNode(Node root){
    
    
    if(Objects.isNull(root)){
    
    
        return;
    }
    Node rightNode = root.rightNode;
    root.rightNode = root.leftNode;
    root.leftNode = rightNode;
    if(root.leftNode != null){
    
    
        reverseNode(root.leftNode);
    }
    if(root.rightNode != null){
    
    
        reverseNode(root.rightNode);
    }
}

5. Flip the binary tree result according to the meaning test

5.1 Create a flip test case

public static void main(String[] args) {
    
    
    Node node = new Node(4);
    ReverseTreeDemo reverTreeDemo = new ReverseTreeDemo();
    reverTreeDemo.createTree(node,2);
    reverTreeDemo.createTree(node,7);
    reverTreeDemo.createTree(node,1);
    reverTreeDemo.createTree(node,3);
    reverTreeDemo.createTree(node,6);
    reverTreeDemo.createTree(node,9);
    System.err.println(node);
    reverTreeDemo.toString(node);
    //翻转树的左右节点
    reverTreeDemo.reverseNode(node);
    System.err.println(node);
    reverTreeDemo.toString(node);
}

5.2 View flip results
insert image description here

5.3 Inorder traversal of binary tree flip results

node-val:9
node-val:7
node-val:6
node-val:4
node-val:3
node-val:2
node-val:1


     4
   /   \
  7     2
 / \   / \
9   6 3   1

6. Complete code

/**
 * 翻转二叉树demo
 * @author senfel
 * @version 1.0
 * @date 2023/5/4 9:38
 */
public class ReverseTreeDemo {
    
    

    /**
     * 创建二叉搜索树
     * 定义 左节点小于父节点,右节点大于父节点
     *
     */
    private void createTree(Node root,int val){
    
    
        if(Objects.isNull(root) || val == 0){
    
    
            return;
        }
        //封装节点
        Node node = new Node(val);
        //节点放入
        setNode(root,node);
    }



    /**
     * 设置节点
     * @param root
     * @param node
     * @author senfel
     * @date 2023/5/4 10:03
     * @return void
     */
    private void setNode(Node root, Node node) {
    
    
       if(node.val > root.val){
    
    
           //右节点
           if(root.rightNode == null){
    
    
               root.rightNode = node;
           }else{
    
    
               setNode(root.rightNode,node);
           }
       }else if(node.val < root.val){
    
    
           //左节点
           if(root.leftNode == null){
    
    
               root.leftNode = node;
           }else{
    
    
               setNode(root.leftNode,node);
           }
       }else{
    
    
           //相等不做处理
           return;
       }
    }

    /**
     * 节点
     */
    static class Node{
    
    
        /**
         * 值
         */
        int val;
        /**
         * 左节点
         */
        Node leftNode;
        /**
         * 右节点
         */
        Node rightNode;

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

    /**
     * 翻转节点
     * 为了符合题意,相当于左右子树交换位置
     * @param root
     * @author senfel
     * @date 2023/5/4 10:37
     * @return void
     */
    private void reverseNode(Node root){
    
    
        if(Objects.isNull(root)){
    
    
            return;
        }
        Node rightNode = root.rightNode;
        root.rightNode = root.leftNode;
        root.leftNode = rightNode;
        if(root.leftNode != null){
    
    
            reverseNode(root.leftNode);
        }
        if(root.rightNode != null){
    
    
            reverseNode(root.rightNode);
        }
    }


    /**
     * 遍历打印二叉搜索树
     * 中序打印
     * @param node
     * @author senfel
     * @date 2023/5/4 10:28
     * @return void
     */
    public void toString(Node node){
    
    
        if(Objects.isNull(node)){
    
    
            return;
        }
        toString(node.leftNode);
        System.err.println("node-val:"+node.val);
        toString(node.rightNode);
    }


    /**
     * 测试用例
     * @param args
     */
    public static void main(String[] args) {
    
    
        Node node = new Node(4);
        ReverseTreeDemo reverTreeDemo = new ReverseTreeDemo();
        reverTreeDemo.createTree(node,2);
        reverTreeDemo.createTree(node,7);
        reverTreeDemo.createTree(node,1);
        reverTreeDemo.createTree(node,3);
        reverTreeDemo.createTree(node,6);
        reverTreeDemo.createTree(node,9);
        System.err.println(node);
        reverTreeDemo.toString(node);
        //翻转树的左右节点
        reverTreeDemo.reverseNode(node);
        System.err.println(node);
        reverTreeDemo.toString(node);
    }

}

Guess you like

Origin blog.csdn.net/weixin_39970883/article/details/130483224