代码随想录算法训练营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;
    }
}
相关推荐
量子炒饭大师1 分钟前
【优化算法:双指针算法刷题宝典】—— 三数之和
算法·优化算法·双指针·三数之和
1104.北光c°4 分钟前
Leetcode215 三种写法完成数组中的第K个最大元素 【hot100算法个人笔记】【java写法】
java·笔记·程序人生·算法·leetcode·排序算法·快速选择
AIpanda88831 分钟前
当数字员工与熊猫智汇协作,如何实现销售潜力的全面提升?
算法
无限进步_31 分钟前
【C++】AVL树完全解析:从平衡因子到四种旋转
c语言·开发语言·数据结构·c++·后端·算法·github
zubylon39 分钟前
前端 RAG:把文档检索接到聊天页
前端·人工智能·算法
Dfreedom.1 小时前
【实战篇】分类任务全流程演示——决策树
人工智能·算法·决策树·机器学习·分类
阿梦Anmory1 小时前
【RAG相关】深入理解混合检索:BM25关键词检索与RRF融合算法详解
算法
浅念-1 小时前
LeetCode最短路必看:BFS算法原理+经典题解
数据结构·c++·算法·leetcode·职场和发展·bfs·宽度优先
aqiu1111111 小时前
ACM校赛
算法
嵌入式小杰1 小时前
一阶低通滤波入门教程:从原理到单片机 C 代码实现
c语言·开发语言·stm32·单片机·算法