创造营第十八天 | ● 513.找树左下角的值● 112. 路径总和 113.路径总和ii● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

找树左下角的值  

题目链接/文章讲解/视频讲解:代码随想录

递归

class Solution {
    int Deep = -1;
    int value;
    public int findBottomLeftValue(TreeNode root) {
        value = root.val;
        findLeftValue(root, 0);
        return value;
    }
    public void findLeftValue(TreeNode root, int deep) {
        if(root == null) return;
        if(root.left == null && root.right == null) {//叶子结点处理
            if(deep > Deep) {
                value = root.val;
                Deep = deep;
            }
        }
        if(root.left != null) {
            findLeftValue(root.left, deep + 1);//deep + 1 包含回溯
        }
        if(root.right != null) {
            findLeftValue(root.right, deep + 1);
        }
    }
}

迭代

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res = 0;
        if(root == null) return res;
        Queue<TreeNode> que = new LinkedList<>();
        que.offer(root);
        while(!que.isEmpty()) {
            int size = que.size();
            for(int i = 0; i < size; i++) {
                TreeNode poll = que.poll();
                if(i == 0) res = poll.val;
                if(poll.left != null) que.offer(poll.left);
                if(poll.right != null) que.offer(poll.right);
            }
        } 
        return res;
    }
}

 路径总和  

题目链接/文章讲解/视频讲解:代码随想录

112. 路径总和

需要注意的是targetSum在递归里是减去结点值 比较其与0的大小

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;
        //对目标值进行减操作 叶子结点直接比较目标值和0的关系
        targetSum -= root.val;
        if(root.left == null && root.right == null) {
            return targetSum == 0;
        }
        if(root.left != null) {
            if(hasPathSum(root.left, targetSum))
            return true;
        }
        if(root.right != null) {
            if(hasPathSum(root.right, targetSum)) {
                return true;
            }
        }
        return false;
    }
}

113. 路径总和ii 

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null) return res;
        getPathSum(root, targetSum, res, path);
        return res;
    }
    public void getPathSum(TreeNode root, int targetSum, List<List<Integer>> res, List<Integer> path) {
        path.add(root.val);
        targetSum -= root.val;//减
        if(root.left == null && root.right == null && targetSum == 0) {
            res.add(new ArrayList<>(path));
        }
        if(root.left != null) {
            getPathSum(root.left, targetSum, res, path);
            path.remove(path.size() - 1);//回溯
        }
        if(root.right != null) {
            getPathSum(root.right, targetSum, res, path);
            path.remove(path.size() - 1);//回溯
        }
    }
}

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

题目链接/文章讲解/视频讲解:代码随想录

106.从中序与后序遍历序列构造二叉树

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 getBulidTree(inorder, 0, inorder.length, postorder, 0, postorder.length);
    }
    public TreeNode getBulidTree(int[] inorder, int inBegin, int inEnd, int[] postorder, int postBegin, int postEnd) {
        if(inBegin >= inEnd || postBegin >= postEnd) {//不符合左闭右开
            return null;
        }
        int rootIndex = postEnd - 1;//后序根节点的索引
        int rootIndex1 = map.get(postorder[rootIndex]);//中序根节点索引
        TreeNode root = new TreeNode(inorder[rootIndex1]);//构造结点
        int lenOfLeft = rootIndex1 - inBegin;//左中序的长度
        root.left = getBulidTree(inorder, inBegin, rootIndex1, postorder, postBegin, postBegin + lenOfLeft);//左子树
        root.right = getBulidTree(inorder, rootIndex1 + 1, inEnd, postorder, postBegin + lenOfLeft, postEnd - 1);//右子树
        return root;
    }
}

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

class Solution {
    Map<Integer, Integer> map;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        map = new HashMap<>();
        for(int i = 0; i < inorder.length; i++) {
            map.put(inorder[i], i);
        }
        return getBulidTree(preorder, 0, preorder.length, inorder, 0, inorder.length);
    }
    public TreeNode getBulidTree(int[] preorder, int preBegin, int preEnd, int[] inorder, int inBegin, int inEnd) {
        if(preBegin >= preEnd || inBegin >= inEnd) {
            return null;
        }
        int rootIndex = map.get(preorder[preBegin]);
        TreeNode root = new TreeNode(preorder[preBegin]);
        int lenOfLeft = rootIndex - inBegin;
        root.left = getBulidTree(preorder, preBegin + 1, preBegin + lenOfLeft + 1, inorder, inBegin, rootIndex);
        root.right = getBulidTree(preorder, preBegin + lenOfLeft + 1, preEnd, inorder, rootIndex + 1, inEnd);
        return root;
    }
}
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值