LeetCode二叉树递归解法大全 +二叉树构建(JAVA实现)

package com.bobo.day56;


import java.util.*;

/**
 * Created by huangbo on 2022/9/26.
 *  leetcode二叉树递归总结
 *  带二叉树构建
 */

public class ErChaShu {

    public static void main(String[] args) {
        String tree = "1,2,3,4,5,6,null";
        //-1代表null
        int[]  numsTree = {1,2,3,4,5,6,-1};
        Integer[] data = {5,4,8,11,null,13,4,7,2,null,null,null,1};
        ErChaShu erChaShu = new ErChaShu();
        TreeNode treeNode = erChaShu.createTree(data,0);

        int n = maxDepth(treeNode);
        System.out.println("二叉树最大深度:"+n);
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
         TreeNode(){

         }
         TreeNode(int val){
             this.val = val;
         }
         TreeNode(TreeNode left,TreeNode right,int val){
             this.val = val;
             this.left = left;
             this.right = right;
         }

    }
    /***
    @description: 字符串生成二叉树
    */
    public TreeNode generateTree(String arrayStr) {
       TreeNode dummyRoot = new TreeNode();
        // null作为虚拟根节点的左孩子
        arrayStr = "null," + arrayStr;
        String[] split = arrayStr.split(",");
        Queue parentQueue = new LinkedList<>();
        parentQueue.add(dummyRoot);
        Queue queue = new LinkedList<>(Arrays.asList(split));

        while (!queue.isEmpty()) {
            // parentQueue出一个
            TreeNode parent = parentQueue.remove();
            // queue出两个
            String leftStr = queue.remove();
            if (!"null".equals(leftStr)) {
                int value = Integer.parseInt(leftStr);
                parent.left = new TreeNode(value);
                parentQueue.add(parent.left);
            }
            if (queue.isEmpty()) break;
            String rightStr = queue.remove();
            if (!"null".equals(rightStr)) {
                int value = Integer.parseInt(rightStr);
                parent.right = new TreeNode(value);
                parentQueue.add(parent.right);
            }
        }

        return dummyRoot.right;
    }
    /***
    @description: int数组构建二叉树  null为-1
    */
    public TreeNode build(int[] arr){
        List list = new ArrayList<>();
        Collections.fill(list, null);
        TreeNode root = null;
        for (int i =0;i res = new ArrayList<>();
    public List preorderTraversal(TreeNode root) {
        qianxu(root);
        return res;

    }
    public void qianxu(TreeNode root){
        if(root == null) return;
        res.add(root.val);
        qianxu(root.left);
        qianxu(root.right);

    }

    /***
    @description: 中序遍历(递归)
    */

    List value = new ArrayList<>();
    public List inorderTraversal(TreeNode root) {
        zhongxu(root);
        return value;
    }

    private void zhongxu(TreeNode root) {
        if(root==null)
            return;
        zhongxu(root.left);
        value.add(root.val);
        zhongxu(root.right);//右子树回来

    }

    /***
    @description: 后续遍历(递归)
    */
    List value2 = new ArrayList();
    public List postorderTraversal(TreeNode root) {
        houxu(root);

        return value2;

    }
    private void houxu(TreeNode root) {

        if (root == null) return;

        houxu(root.left);
        houxu(root.right);
        value2.add(root.val);
    }

    /***
    @description: 二叉树层序遍历(递归)
    */
    List>  lists = new ArrayList<>();
    public List> levelOrder(TreeNode root) {
        if(root == null) return lists;
        diguiCX(root,0);
        return lists;
    }

    public void diguiCX(TreeNode node,int level){
        if(level>=lists.size()){
            lists.add(new ArrayList());
        }
        lists.get(level).add(node.val);
        if(node.left!=null){
            diguiCX(node.left,level+1);
        }
        if(node.right!=null){
            diguiCX(node.right,level+1);
        }
    }
    /***
    @description: 对称二叉树
    */
    public boolean isSymmetric(TreeNode root) {

            return check(root,root);
    }

    public boolean check(TreeNode p,TreeNode q){
        if (p==null&&q==null) return true;
        if (p==null||q==null) return false;

        return p.val==q.val&&check(p.left,q.right)&&check(p.right,q.left);
    }

    /***
    @description: 翻转二叉树
    */

    public TreeNode invertTree(TreeNode root) {
        if (root==null){
            return null;
        }
        //这里用前序  后序也可以 中序不行
        swapChildren(root);
        invertTree(root.left);
        invertTree(root.right);

        return root;

    }

    private void swapChildren(TreeNode root) {
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
    }

    /***
    @description: 二叉树最大深度(递归)
    */

    public static int maxDepth(TreeNode root) {

        if (root==null){
            return 0;
        }

        int leftDepth = maxDepth(root.left);
//        System.out.println(leftDepth);
        int rightDepth = maxDepth(root.right);
//        System.out.println(rightDepth);

        return Math.max(leftDepth,rightDepth)+1;

    }

    /***
    @description: 二叉树最小深度(递归)
    */
    public int minDepth(TreeNode root) {

        if (root == null){
            return 0;
        }
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if (root.left==null&&root.right!=null){
            return rightDepth+1; //最小深度是右子树的最小深度+根 也就是1
        }
        if (root.right==null&&root.left!=null){
            return leftDepth+1; 最小深度是左子树的最小深度+根 也就是1
        }

        // 左右结点都不为null
        return Math.min(leftDepth, rightDepth) + 1;

    }

    /***
    @description: 完全二叉树的节点个数(递归)
    */

    // 通用递归解法 即二叉树的节点数
    public static int countNodes(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }


    /***
    @description: 判断是否为平衡二叉树(递归)
    */
    public boolean isBalanced(TreeNode root) {
        return getHeight(root) != -1;
    }

    private int getHeight(TreeNode root) {
        if(root==null){
            return 0;
        }
        int leftHeight = getHeight(root.left);
        if (leftHeight==-1){
            return -1;
        }
        int rightHeight = getHeight(root.right);
        if (rightHeight==-1){
            return -1;
        }

        return Math.abs(leftHeight-rightHeight)>1?-1:Math.max(leftHeight,rightHeight)+1;
    }

    /***
    @description: 左叶子之和(递归)
    */
    public int sumOfLeftLeaves(TreeNode root) {
        if (root == null) return 0;
        int leftValue = sumOfLeftLeaves(root.left);    // 左
        int rightValue = sumOfLeftLeaves(root.right);  // 右

        int midValue = 0;
        if (root.left != null && root.left.left == null && root.left.right == null) {
            midValue = root.left.val;
        }
        int sum = midValue + leftValue + rightValue;  // 中
        return sum;
    }

    /***
    @description: 验证是否为二叉搜索树(递归)
    */
    // 递归
    TreeNode max;
    public boolean isValidBST(TreeNode root) {
        //中序遍历 左中右 一旦发现根的值小于左子树的值
        if (root == null) {
            return true;
        }
        // 左
        boolean left = isValidBST(root.left);
        if (!left) {
            return false;
        }
        // 中
        if (max != null && root.val <= max.val) {
            return false;
        }
        max = root;
        // 右
        boolean right = isValidBST(root.right);

        return right;
    }

}

你可能感兴趣的:(leetcode,算法,java)