代码随想录算法训练营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;
    }
}
相关推荐
zh路西法7 分钟前
【宇树机器人强化学习】(一):PPO算法的python实现与解析
python·深度学习·算法·机器学习·机器人
随意起个昵称10 分钟前
【贪心】选择尽量多的不相交区间
数据结构·算法
章小幽20 分钟前
LeetCode-35.搜索插入位置
数据结构·算法·leetcode
放下华子我只抽RuiKe535 分钟前
机器学习全景指南-探索篇——发现数据内在结构的聚类算法
人工智能·深度学习·算法·机器学习·语言模型·数据挖掘·聚类
Yupureki1 小时前
《C++实战项目-高并发内存池》3.ThreadCache构造
服务器·c语言·c++·算法·哈希算法
j_xxx404_1 小时前
C++算法:一维/二维前缀和算法模板题
开发语言·数据结构·c++·算法
x_xbx1 小时前
LeetCode:111. 二叉树的最小深度
算法·leetcode·职场和发展
入目星河滚烫1 小时前
网易互娱2020校招在线笔试—游戏研发第一批—游泳池-研发
算法·笔试·数据结构与算法
xier_ran1 小时前
【第一周】关键词解释:倒数排名融合(Reciprocal Rank Fusion, RRF)算法
开发语言·python·算法
spiritualfood1 小时前
蓝桥杯大学b组水质检测
c语言·c++·算法·青少年编程·职场和发展·蓝桥杯