首页 > 其他分享 >代码随想录18 513.找树左下角的值 | 112. 路径总和 113.路径总和ii | 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

代码随想录18 513.找树左下角的值 | 112. 路径总和 113.路径总和ii | 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

时间:2023-03-18 22:12:44浏览次数:54  
标签:遍历 TreeNode val int right 二叉树 序列 root left

513. 找树左下角的值

给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。

假设二叉树中至少有一个节点。

示例 1:

输入: root = [2,1,3]
输出: 1
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    int max = Integer.MIN_VALUE;
    int result = 0;
    private void helper(TreeNode root, int depth) {
        if (root.left == null && root.right == null) {
            if (depth > max) {
                max = depth;
                result = root.val;
            }
            return;
        }

        if (root.left != null) {
            depth++;
            helper(root.left, depth);
            depth--;
        }
        if (root.right != null) {
            depth++;
            helper(root.right, depth);
            depth--;
        }
    }
    public int findBottomLeftValue(TreeNode root) {
        helper(root, 0);
        return result;
    }
}

112. 路径总和

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false 。

叶子节点 是指没有子节点的节点。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if (root == null) return false;
        targetSum -= root.val;
        if (root.left == null && root.right == null) {
            return targetSum == 0;
        }
        if (root.left != null) {
            boolean left = hasPathSum(root.left, targetSum);
            if (left) {
                return true;
            }
        }
        if (root.right != null) {
            boolean right = hasPathSum(root.right, targetSum);
            if (right) {
                return true;
            }
        }
        return false;
    }
}

  

113. 路径总和ii

力扣题目链接(opens new window)

给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径。

说明: 叶子节点是指没有子节点的节点。

示例: 给定如下二叉树,以及目标和 sum = 22,

 

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        if (root == null) return res; // 非空判断

        List<Integer> path = new LinkedList<>();
        preorderdfs(root, targetSum, res, path);
        return res;
    }

    public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) {
            path.add(root.val);

            if (root.left == null && root.right == null) {
            // 找到了和为 targetsum 的路径
                if (targetsum - root.val == 0) {
                    res.add(new ArrayList<>(path));
                }
                return; // 如果和不为 targetsum,返回
            }

            if (root.left != null) {
               preorderdfs(root.left, targetsum - root.val, res, path);
               path.remove(path.size() - 1);
            }
             if (root.right != null) {
            preorderdfs(root.right, targetsum - root.val, res, path);
            path.remove(path.size() - 1); // 回溯
        }
    }
    
}

105
给定两个整数数组 inorder 和 postorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。

class Solution {
    Map<Integer, Integer> map;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        map = new HashMap<>();
       for (int i = 0; i < inorder.length; i++) {
           map.put(inorder[i], i);
       }
       return findNode(inorder,  0, inorder.length, postorder,0, postorder.length);
    }

    private TreeNode findNode(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
        if (inBegin >= inEnd || postBegin >= postEnd) {  // 不满足左闭右开,说明没有元素,返回空树
            return null;
        }
         int rootIndex = map.get(postorder[postEnd - 1]); 
           TreeNode root = new TreeNode(inorder[rootIndex]);  // 构造结点
        int lenOfLeft = rootIndex - inBegin;  // 保存中序左子树个数,用来确定后序数列的个数
        root.left = findNode(inorder, inBegin, rootIndex,
                            postorder, postBegin, postBegin + lenOfLeft);
        root.right = findNode(inorder, rootIndex + 1, inEnd,
                            postorder, postBegin + lenOfLeft, postEnd - 1);

        return root;

    }
}

 

标签:遍历,TreeNode,val,int,right,二叉树,序列,root,left
From: https://www.cnblogs.com/libertylhy/p/17231963.html

相关文章

  • List如何一边遍历,一边删除?
    使用Iterator的remove()方法:每次删除一个元素,都会将modCount的值重新赋值给expectedModCount,这样2个变量就相等了,不会触发java.util.ConcurrentModificationException异常......
  • django序列化多对多字段设置为空时需要在模型表多对多字段加blank=True这个参数
    menus=models.ManyToManyField(to='Menu',db_table='lqz_roles_menus',blank=True)如果不写在存空列表的时候会报错写了......
  • PHP&JAVA反序列化
    PHP反序列化:原理:序列化就是将对象转为字符串。反序列化与之相反,数据的格式的转换对象的序列化利于对象的保存和传输,也可以让多个文件共享对象技术:有类(触发魔术方法);无类......
  • B.小红的子序列(dp)
    B.小红的子序列(dp)题目链接自序列问题一般是dp问题,这里结尾dp状态只有四种,蓝偶,红偶,蓝奇,红奇。对于当前物品,所要做的判断就是加与不加入状态完全相反的背包中,例如,当前是......
  • R语言k-Shape时间序列聚类方法对股票价格时间序列聚类|附代码数据
    原文链接:http://tecdat.cn/?p=3726最近我们被客户要求撰写关于k-Shape时间序列聚类的研究报告,包括一些图形和统计输出。本文我们将使用k-Shape时间序列聚类方法检查与......
  • 操作码序列
    操作码序列通常对PE格式文件(.exe文件等),用IDAPro反汇编得到对应的asm(包含汇编代码)文件。从asm文件中可以提取操作码、函数调用等信息作为特征训练机器学习和深度......
  • 代码随想录训练营day 14||二叉树理论基础篇、二叉树的递归遍历、二叉树的迭代遍历
    二叉树理论基础篇二叉搜索树前面介绍的树,都没有数值的,而二叉搜索树是有数值的了,二叉搜索树是一个有序树。若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值......
  • 【特征】操作码序列
    【特征】操作码序列通常对PE格式文件(.exe文件等),用IDAPro反汇编得到对应的asm(包含汇编代码)文件。从asm文件中可以提取操作码、函数调用等信息作为特征训练机器学......
  • 【小结】操作码序列
    【小结】操作码序列通常对PE格式文件(.exe文件等),用IDAPro反汇编得到对应的asm(包含汇编代码)文件。从asm文件中可以提取操作码、函数调用等信息作为特征训练机器学......
  • bs4介绍,遍历文档树-bs4搜索文档树-css选择器-selenium基本使用-无界面浏览器-selenium
    目录bs4介绍,遍历文档树-bs4搜索文档树-css选择器-selenium基本使用-无界面浏览器-selenium其他用法昨日回顾今日内容详细0bs4介绍遍历文档树0.1bs4的遍历文档树1bs4搜......