代码随想录算法训练营day18 513.找到左下角的值 112.路径总和 113.路径总和 105.从前序与中序构建二叉树 106.从后序与中序构建二叉树

题目链接513.找到左下角的值

class Solution {
    private int Deep = -1;
    private int value = 0;
    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(deep>Deep){
            value = root.val;
            Deep = deep;
        }
        if(root.left != null){
            findLeftValue(root.left, deep + 1);
        }
        if(root.right != null){
            findLeftValue(root.right, deep + 1);
        }
    }
}

题目链接112.路径总和

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return false;
        }
        
        int sum = root.val;
        if(root.left == null && root.right == null ){
            return 0 == targetSum-sum;
        }
        if (root.left != null) {
            boolean left = hasPathSum(root.left, targetSum-sum);
            if (left) {      // 已经找到
                return true;
            }
        }
        if (root.right != null) {
            boolean right = hasPathSum(root.right, targetSum-sum);
            if (right) {     // 已经找到
                return true;
            }
        }
        return false;
    }
}

题目链接113.路径总和||

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 ArrayList<>();
        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){
            if(0 == targetSum-root.val){
                res.add(new ArrayList<>(path));
            }
            return;
        }
        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.从前序与中序构建二叉树

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保存中序序列的数值对应位置
            map.put(inorder[i], i);
        }

        return findNode(preorder, 0, preorder.length, inorder,  0, inorder.length);  // 前闭后开
    }

    public TreeNode findNode(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(inorder[rootIndex]);  // 构造结点
        int lenOfLeft = rootIndex - inBegin;  // 保存中序左子树个数,用来确定前序数列的个数
        root.left = findNode(preorder, preBegin + 1, preBegin + lenOfLeft + 1,
                            inorder, inBegin, rootIndex);
        root.right = findNode(preorder, preBegin + lenOfLeft + 1, preEnd,
                            inorder, rootIndex + 1, inEnd);

        return root;
    }
}

题目链接106.从后序与中序构建二叉树

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值