【LeetCode】初级算法:树

本文深入探讨了二叉树的五大核心算法:最大深度计算、验证二叉搜索树、判断对称性、层次遍历及有序数组转二叉搜索树。通过具体实现示例,帮助读者理解每种算法的工作原理及其应用场景。

1. 二叉树的最大深度

用时:0ms

class Solution {
    public int maxDepth(TreeNode root) {
        // 返回左右子树深度的最大值,再加1
        if(root!=null){
            return Math.max(maxDepth(root.left),maxDepth(root.right))+1;
        }
        return 0;
    }
}

2. 验证二叉搜索树

用时:3ms

class Solution {
    public boolean isValidBST(TreeNode root) {
        ArrayList<Integer> array=new ArrayList<>();
        func(root,array);
        for(int i=1;i<array.size();++i){
            // 两个元素相同时为false
            if(array.get(i-1)>=array.get(i)){
                return false;
            }
        }
        return true;
    }

    // 中序遍历后数组为升序即为true
    public void func(TreeNode root,ArrayList<Integer> array){
        if(root!=null){
            func(root.left,array);
            array.add(root.val);
            func(root.right,array);
        }
    }
}

3. 对称二叉树

用时:11ms

class Solution {
    public boolean isSymmetric(TreeNode root) {
        ArrayList<Integer> array=new ArrayList<>();
        StringBuilder direcStr=new StringBuilder();
        inOrder(root,array,direcStr,'1');
        int i=0,j=direcStr.length()-1;
        while(i<j){
            // 如果方向相同,或者数组不是回文,则不是对称
            if(direcStr.charAt(i)==direcStr.charAt(j)||array.get(i)!=array.get(j)){
                return false;
            }
            ++i;
            --j;
        }
        return true;
    }
    
    // 中序遍历后数组是回文的,并且方向也是回文的
    public void inOrder(TreeNode root,ArrayList<Integer> array,StringBuilder direcStr,char direc){
        if(root!=null){
            inOrder(root.left,array,direcStr,'1');
            array.add(root.val);
            direcStr.append(direc);
            inOrder(root.right,array,direcStr,'0');
        }
    }
}

4. 二叉树的层次遍历

用时:1ms

class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> outList=new ArrayList<>();
        inOrder(root,outList,0);
        return outList;
    }
    
    // 先序遍历的同时将每一层的结点保存到对应层次的列表
    public void inOrder(TreeNode root,List<List<Integer>> outList,int deepth){
        if(root==null) return;
        
        // 如果深度大于外列表长度,表示左边没有右边深
        while(outList.size()<=deepth){
            outList.add(new ArrayList<Integer>());
        }
        
        outList.get(deepth).add(root.val);
        inOrder(root.left,outList,deepth+1);
        inOrder(root.right,outList,deepth+1);
    }
}

5. 将有序数组转换为二叉搜索树

用时:1ms

class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
        return convert(nums,0,nums.length-1);
    }
    
    // 每次都将子数组的中间值作为子树根节点
    public TreeNode convert(int[] nums,int s,int e){
        if(s>e) return null;
        int center=(s+e+1)/2; // 根据示例,偶数数组以右边作为center
        TreeNode curRoot=new TreeNode(nums[center]);
        curRoot.left=convert(nums,s,center-1);
        curRoot.right=convert(nums,center+1,e);
        return curRoot;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值