代码随想录算法训练营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;
    }
}
相关推荐
nick987616 分钟前
信号处理之中值滤波
人工智能·算法·信号处理
宇宙超粒终端控制中心32 分钟前
leetcode34. 在排序数组中查找元素的第一个和最后一个位置
算法·leetcode·二分查找
sp_fyf_20241 小时前
计算机前沿技术-人工智能算法-大语言模型-最新研究进展-2024-10-01
人工智能·神经网络·算法·语言模型·自然语言处理·数据挖掘
yi碗汤园1 小时前
C#自定义工具类-数组工具类
开发语言·算法·c#
hunteritself1 小时前
ChatGPT实时语音将于本周向免费用户推出:OpenAI DevDay 2024详细解读
人工智能·gpt·算法·chatgpt·openai·语音识别
脑子不好真君1 小时前
线性代数书中求解齐次线性方程组、非齐次线性方程组方法的特点和缺陷(附实例讲解)
人工智能·线性代数·算法
大磊程序员(“hello world”)2 小时前
35.搜索插入位置
数据结构·算法·leetcode
宇宙超粒终端控制中心2 小时前
leetcode69--x的平方根
java·数据结构·算法·leetcode·二分查找
万物皆可Hook2 小时前
咸鱼sign逆向分析与爬虫实现
javascript·爬虫·python·算法
luluvx2 小时前
LeetCode[中等] 55.跳跃游戏
算法·leetcode·游戏