算法刷题|513.找树左下角的值、112.路径总和、113.路径总和||、106.从中序与后序遍历序列构造二叉树、105.从前序与中序遍历序列构造二叉树

找树左下角的值

题目:给定一个二叉树的 根节点 root,请找出该二叉树的 最底层 最左边 节点的值。
假设二叉树中至少有一个节点。

递归

思路:

  1. 左下角的意思是最底层最左边的节点,这个节点可能是左孩子也可能是右孩子。
  2. 本题前中后序遍历都可以使用,因为没有对中进行操作
  3. 我们需要一个变量res记录最左孩子的值(最左边),还需要一个变量maxDepth记录递归的深度(最底层)
  4. 当递归的当前节点的左右孩子为null的时候,并且当前的深度大于目前记录的最大深度的时候,我们就更新深度和结果值
  5. 向左递归
  6. 向右递归
class Solution {
    int res = 0;
    int maxDepth = Integer.MIN_VALUE;
    public int findBottomLeftValue(TreeNode root) {
        getLeftValue(root,1);
        return res;
    }
    private void getLeftValue(TreeNode node,int depth){
        if(node.left == null && node.right == null){// 终止条件 当前是叶子节点
            // 如果当前的深度大于记录的最大深度,就更新深度和叶子节点的值
            if(depth > maxDepth){// 这里只能是大于,不能是等于,因为如果是等于就会把同一层的右边的值更新进去,这样就不是最左边了
                maxDepth = depth;
                res = node.val;
            }
        }
        // 左
        if(node.left != null){
            depth++;// 进入下一层递归,也就是树的深度+1
            getLeftValue(node.left,depth);
            depth--;// 回溯
        }
        // 右
        if(node.right != null){
            depth++;// 进入下一层递归,也就是树的深度+1
            getLeftValue(node.right,depth);
            depth--;// 递归
        }
        // 中
        // 不需要处理
    }
}

迭代

思路:使用层序遍历,然后将最后一层的第一个节点返回就行了

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res = 0;
        int maxDepth = Integer.MIN_VALUE;
        if(root == null){
            return res;
        }
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.addLast(root);
        int curDepth = 0;
        while(!deque.isEmpty()){
            int size = deque.size();
            curDepth++;
            while(size --> 0){
                TreeNode node = deque.pollFirst();
                if(curDepth > maxDepth){// 如果当前深度大于记录的最大深度就更新值
                    res = node.val;
                    maxDepth = curDepth;
                }
                if(node.left != null){
                    deque.addLast(node.left);
                }
                if(node.right != null){
                    deque.addLast(node.right);
                }
            }
        }
        return res;
    }
}

优化一下代码

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res = 0;
        if(root == null){
            return res;
        }
        Deque<TreeNode> deque = new ArrayDeque<>();
        deque.addLast(root);
        while(!deque.isEmpty()){
            int size = deque.size();
            res = deque.peekFirst().val;// 每一个外层循环都是从每一层的第一个节点开始的,刚好我们要找的就是这个节点
            while(size --> 0){
                TreeNode node = deque.pollFirst();
                if(node.left != null){
                    deque.addLast(node.left);
                }
                if(node.right != null){
                    deque.addLast(node.right);
                }
            }
        }
        return res;
    }
}

路径总和

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

思路:因为没有中节点做处理,所以本题可以采用前中后序遍历。因为本题是需要走所有的路径(不一定),到了叶子节点之后还需要回退到父节点,然后继续遍历父节点的右孩子,所以涉及到回溯

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return false;
        }
       return getPathSum(root,targetSum - root.val);
    }
    private boolean getPathSum(TreeNode node,int count){
        if(node.left == null && node.right == null){// 终止条件 当前在叶子节点,判断目标值被减之后是否等于当前叶子节点的值,等于就说明存在路径和等于目标值
            return count == 0;
        }
        // 左
        if(node.left != null){
            count-=node.left.val;
            if(getPathSum(node.left,count)){
                return true;
            }
            count+=node.left.val;// 回溯
        }
        // 右
        if(node.right != null){
            count-=node.right.val;
            if(getPathSum(node.right,count)){
                return true;
            }
            count+=node.right.val;// 回溯
        }
        // 中
        // 无需处理
        return false;
    }
}

路径总和||

题目:给你二叉树的根节点 root 和一个整数目标和 targetSum ,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。

思路:和上面的思路基本一样,就是在找到符合条件的路径的时候不要返回了而是加入到结果集中,当然我们需要定一个集合path记录路径和一个集合resList记录路径集合

class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> resList = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        if(root == null){
            return resList;
        }
        path.add(root.val);// 先将根节点放进去
        getPath(root,targetSum-root.val,path,resList);// 因为递归的结束条件是判断count是否等于0,所有这里需要先将根节点的值减去,后面进递归的之前也是先将即将进入递归的节点的值减掉
        return resList;
    }
    private void getPath(TreeNode node,int count,List<Integer> path,List<List<Integer>> resList){
        if(node.left == null && node.right == null){// 终止条件
            if(count == 0){
                resList.add(new ArrayList(path));
            }
            return;
        }
        if(node.left != null){
            count-=node.left.val;
            path.add(node.left.val);// 收集路径节点
            getPath(node.left,count,path,resList);
            count+=node.left.val;// 回溯
            path.remove(path.size()-1);// 回溯
        }
        if(node.right != null){
            count-=node.right.val;
            path.add(node.right.val);
            getPath(node.right,count,path,resList);
            count+=node.right.val;
            path.remove(path.size()-1);
        }
    }
}

从中序与后序序列构造二叉树

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

思路

  1. 后序数组的最后一个元素一定是根节点(这个根节点是相对来说的可能是子树的根节点,也可能是整个树的根)
  2. 通过上面找到根节点去切割中序数组
  3. 然后根据切割好的中序的左子树的中序和右子树的中序数组的大小去切割后序数组
  4. 然后构建当前根节点的左右子树
class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(postorder.length == 0){// 终止条件,当拆分后的后序数组的大小为0的时候,说明到了叶子节点的孩子节点,可以直接返回null了
            return null;
        }
        // 后序数组的最后一个元素就是根节点的值
        int rootVal = postorder[postorder.length - 1];
        // 构造根节点
        TreeNode root = new TreeNode(rootVal);
        if(postorder.length == 1 ){// 终止条件,说明递归到了叶子节点
            return root;
        }
        // 找到根节点在中序数组中的位置,方便切割数组
        int index = 0;
        for(;index<inorder.length;index++){
            if(inorder[index] == rootVal){
                break;
            }
        }
        // 切割中序数组
        // 左中序
        int[] leftInorder = new int[index];
        for(int i = 0;i<index;i++){
            leftInorder[i] = inorder[i];
        }
        // 右中序
        int[] rightInorder = new int[inorder.length - index - 1];
        for(int i = 0;i<rightInorder.length;i++){
            rightInorder[i] = inorder[index+i+1];
        }

        // 切割后序数组
        // 左后序
        int[] leftPostorder = new int[index];
        for(int i = 0;i<leftInorder.length;i++){
            leftPostorder[i] = postorder[i];
        }
        // 右中序
        int[] rightPostorder = new int[postorder.length - index - 1];
        for(int i = 0;i<rightInorder.length;i++){
            rightPostorder[i] = postorder[index+i];
        }
        // 构造左子树
        root.left = buildTree(leftInorder,leftPostorder);
        // 构造右子树
        root.right = buildTree(rightInorder,rightPostorder);
        return root;
    }
}

从前序与中序遍历序列构造二叉树

题目:根据一棵树的前序遍历与中序遍历构造二叉树。注意: 你可以假设树中没有重复的元素。

思路:

  1. 前序数组的第一个元素一定是根节点
  2. 通过根节点的值找到中序数组的根节点位置,便于切割中序数组
  3. 将中序数组切割成左中序和右中序数组
  4. 根据切割好的左中序和右中序数组的大小来切割前序数组
  5. 递归构造左子树
  6. 递归构造右子树
  7. 返回根节点
class Solution {
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder.length == 0){// 终止条件,到了叶子节点的孩子节点
            return null;
        }
        // 根节点的值
        int rootVal = preorder[0];
        // 构造根节点
        TreeNode root = new TreeNode(rootVal);
        if(preorder.length == 1){// 终止条件,到了叶子节点
            return root;
        }
        // 找到中序数组切割的点
        int index = 0;
        for(;index<inorder.length;index++){
            if(inorder[index] == rootVal){
                break;
            }
        }
        // 切割中序数组
        // 左中序
        int[] leftInorder = new int[index];
        for(int i = 0;i<index;i++){
            leftInorder[i] = inorder[i];
        }
        // 右中序
        int[] rightInorder = new int[inorder.length - index - 1];
        for(int i = 0;i<rightInorder.length;i++){
            rightInorder[i] = inorder[index + i + 1];
        }

        // 切割前序数组
        // 左前序
        int[] leftPreorder = new int[leftInorder.length];
        for(int i = 0;i<leftPreorder.length;i++){
            leftPreorder[i] = preorder[i+1];
        }
        // 右前序
        int[] rightPreorder = new int[rightInorder.length];
        for(int i = 0;i<rightPreorder.length;i++){
            rightPreorder[i] = preorder[leftPreorder.length+i+1];
        }
        // 构造当前节点的左子树
        root.left = buildTree(leftPreorder,leftInorder);
        // 构造当前节点的右子树
        root.right = buildTree(rightPreorder,rightInorder);
        return root;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值