代码随想录算法训练营Day14

513.找树左下角的值

力扣题目链接:. - 力扣(LeetCode)

层序遍历

java 复制代码
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        if(root==null){
            return 0;
        }
        Deque<TreeNode> myque=new LinkedList<>();
        myque.offer(root);
        int count=0;
        while(!myque.isEmpty()){
            count++;
            int len=myque.size();
            while(len>0){
                TreeNode cur=myque.poll();
                if(cur.left!=null){
                    myque.offer(cur.left);
                }
                if(cur.right!=null){
                    myque.offer(cur.right);
                }
                len--;
            }
        }
        int count1=1;
        myque.offer(root);
        while(count1<count){
            count1++;
            int len=myque.size();
            while(len>0){
                TreeNode cur=myque.poll();
                if(cur.left!=null){
                    myque.offer(cur.left);
                }
                if(cur.right!=null){
                    myque.offer(cur.right);
                }
                len--;
            }
        }
        TreeNode cur2=myque.poll();
        return cur2.val;
    }
}

112. 路径总和

力扣题目链接:. - 力扣(LeetCode)

前序递归

java 复制代码
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root==null){
            return false;
        }
        List<Integer> path=new ArrayList<>();     
        return hasSum(root,path,targetSum);
    }
    public boolean hasSum(TreeNode root,List<Integer> path,int targetSum){
        path.add(root.val);
        if(root.left==null&&root.right==null){
            int sum=0;
            for(Integer i:path){
                sum+=i;
            }
            if(sum==targetSum)
            return true;
            return false;
        }
        if(root.left!=null){
            boolean has=hasSum(root.left,path,targetSum);
            if(has)
            return has;
            path.remove(path.size()-1);
        }
        if(root.right!=null){
            boolean has=hasSum(root.right,path,targetSum);
            if(has)
            return has;
            path.remove(path.size()-1);
        }
        return false;
    }

}

106.从中序与后序遍历序列构造二叉树

力扣题目链接. - 力扣(LeetCode)

循环不变量,前序递归

java 复制代码
class Solution {
    Map<Integer,Integer> inordermap;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
         inordermap=new HashMap<>();
        for(int i=0;i<inorder.length;i++){
            inordermap.put(inorder[i],i);
        }
        return fondNode(inorder,0,inorder.length,postorder,0,postorder.length);
    }
    public TreeNode fondNode(int[] inorder,int instart,int inend,int[]postorder,int poststart,int postend){
        if(instart>=inend||poststart>=postend){
            return null;
        }
        int inindex=inordermap.get(postorder[postend-1]);
        TreeNode root=new TreeNode(postorder[postend-1]);
        int leftLen=inindex-instart;
        root.left=fondNode(inorder,instart,inindex,postorder,poststart,poststart+leftLen);
        root.right=fondNode(inorder,inindex+1,inend,postorder,poststart+leftLen,postend-1);
        return root;
    }
}
相关推荐
强盛小灵通专卖员3 小时前
分类分割详细指标说明
人工智能·深度学习·算法·机器学习
IT猿手6 小时前
基于强化学习 Q-learning 算法求解城市场景下无人机三维路径规划研究,提供完整MATLAB代码
神经网络·算法·matlab·人机交互·无人机·强化学习·无人机三维路径规划
万能程序员-传康Kk9 小时前
旅游推荐数据分析可视化系统算法
算法·数据分析·旅游
PXM的算法星球10 小时前
【并发编程基石】CAS无锁算法详解:原理、实现与应用场景
算法
ll77881110 小时前
C++学习之路,从0到精通的征途:继承
开发语言·数据结构·c++·学习·算法
烨然若神人~10 小时前
算法第十七天|654. 最大二叉树、617.合并二叉树、700.二叉搜索树中的搜索、98.验证二叉搜索树
算法
爱coding的橙子10 小时前
每日算法刷题Day2 5.10:leetcode数组1道题3种解法,用时40min
算法·leetcode
程序媛小盐11 小时前
贪心算法:最小生成树
算法·贪心算法·图论
Panesle11 小时前
分布式异步强化学习框架训练32B大模型:INTELLECT-2
人工智能·分布式·深度学习·算法·大模型
多多*11 小时前
算法竞赛相关 Java 二分模版
java·开发语言·数据结构·数据库·sql·算法·oracle