代码随想录day18

代码随想录day18

513.找树左下角的值

此题我们采用层序遍历的方法。返回最底层的左边值即是我们所求的值
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        LinkedList<TreeNode> list =  new LinkedList<>();
        list.offer(root);
        int ans = 0;
        while(!list.isEmpty()){
            int size = list.size();
            for(int i = 0 ; i < size ; i++){
                TreeNode node = list.poll();
                if(i == 0){
                    ans = node.val;
                }
                if(node.left != null){
                    list.offer(node.left);
                }
                if(node.right != null){
                    list.offer(node.right);
                }
            }
        }

        return ans;
    }
}

112.路径总和

此题使用一个递归的思想,每遇到一个节点就另目标值减去此节点的值,遇到叶子节点时进行判断是否为零,为零则符合题意返回true
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return false;
        }
        boolean right = false;
        boolean left = false;
        targetSum -= root.val;  //减去节点的值

        //如果是叶子节点,判断此时target是否为0
        if(root.left == null && root.right == null){
            return targetSum == 0;
        }

        //非叶子节点递归该节点的左子树和右子树
        if(root.left != null){
            left = hasPathSum(root.left,targetSum);
        }

        if(root.right != null){
            right = hasPathSum(root.right,targetSum);
        }

        //若left or right 中有一值为true则说明有符合条件的路径
        if(left || right) return true;

        return false;
    }
}

113.路径总和II

class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        
        if(root == null){
            return ans;
        }

        path(root , targetSum);
        return ans;

    }

    void path(TreeNode root , int targetSum){
        path.add(root.val);
        targetSum -= root.val;
        //叶子节点且路径和符合我们的目标值,返回此时的路径
        if(targetSum == 0 && root.left == null && root.right == null){
            ans.add(new ArrayList(path));
        }
        //对于非叶子节点,进行迭代回溯
        if(root.left != null){
            path(root.left,targetSum);
            path.removeLast();
        }
        if(root.right != null){
            path(root.right,targetSum);
            path.removeLast();
        }
    }
}

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

  1. 后序数组的最后一位元素就是我们的根节点
  2. 根据根节点将中序数组分为左右两部分
  3. 左部分就是我们的根节点的左子树部分,另其反复执行上述两个程序(递归)
  4. 右子树同左子树
    由于整个过程中我们的所有的计数均从0开始,所以索引下标取不到length的值,故使用左闭右开
class Solution {
    Map<Integer , Integer> map;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        map = new HashMap<>();
        //将前序数组放入map中
        for(int i = 0 ; i < inorder.length ; i++){
            map.put(inorder[i], i);
        }

        return tree(inorder ,  0 , inorder.length , postorder , 0 , postorder.length);
    }

    public TreeNode tree(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 len = rootIndex - inBegin; //左子树的个数
        //递归我们的左右子树
        root.left = tree(inorder,inBegin,rootIndex,
                         postorder,postBegin,postBegin+len);

        root.right = tree(inorder,rootIndex+1,inEnd,
                         postorder,postBegin+len,postEnd-1);
    
        return root;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值