leetcode 刷题 二叉树

513. 找树左下角的值

 这道题目 使用层序遍历比较方便简单 也比较好想到


class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        int res = 0;
        queue.offer(root);

        while(!queue.isEmpty()){
            int size = queue.size();

            for(int i = 0; i < size; i++){
                TreeNode node = queue.poll();
                if(i == 0){
                    res = node.val;
                }
                if(node.left != null){
                    queue.offer(node.left);
                }
                if(node.right != null){
                    queue.offer(node.right);
                }
            }
        }
        return res;

    }
}

递归的方法 可以使用回溯

class Solution {
    int depth = -1;
    int Deep = 0;
    int res = 0;
    public int findBottomLeftValue(TreeNode root) {
       findValue(root);
       return res;
    }

    private void findValue(TreeNode root){
        if(root == null){
            return;
        }
        if (root.left == null && root.right == null) {
            if (Deep > depth) {
                res = root.val;
                depth = Deep;
            }
        }
        if(root.left != null){
            Deep++;
            findValue(root.left);
            Deep--;
        }
        if(root.right != null){
            Deep++;
            findValue(root.right);
            Deep--;
        }
    }
}

112. 路径总和

回溯题

一般回溯都不需要返回值

但是对于这道题,找到了一条符合要求的路径就可以返回,不需要在递归下去了,所以回溯函数需要返回值来提前结束递归

  • 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。(本题的情况)
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null){
            return false;
        }
        return backtracking(root, root.val, targetSum);
    }

    private boolean backtracking(TreeNode root, int sum, int targetSum){
        if(root.left == null && root.right == null){
            if(sum == targetSum){
                return true;
            }else{
                return false;
            }
        }

        if(root.left != null){
            sum += root.left.val;
            if(backtracking(root.left, sum, targetSum)){
                return true;
            }
            sum -= root.left.val;
        }

        if(root.right != null){
            sum += root.right.val;
            if(backtracking(root.right, sum, targetSum)){
                return true;
            }
            sum -= root.right.val;
        }
        return false;
    }
}

 113. 路径总和 II

 回溯法

是另外一种情况  需要找到所有符合要求的解

所以需要遍历全部的节点 不需要提前返回

所以回溯函数不需要返回值

  • 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。(这种情况就是本文下半部分介绍的113.路径总和ii)
class Solution {
    List<Integer> path = new ArrayList<>();
    List<List<Integer>> res = new ArrayList<>();

    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null){
            return res;
        }
        path.add(root.val);
        backtracking(root, root.val, targetSum, path);
        return res;
    }

    private void backtracking(TreeNode root, int sum, int targetSum, List<Integer> path){
        if(root.left == null && root.right == null){
            if(sum == targetSum){
                res.add(new ArrayList<>(path));
            }
            return;
        }

        if(root.left != null){
            sum += root.left.val;
            path.add(root.left.val);
            backtracking(root.left, sum, targetSum, path);
            sum -= root.left.val;
            path.remove(path.size() - 1);
        }

        if(root.right != null){
            sum += root.right.val;
            path.add(root.right.val);
            backtracking(root.right, sum, targetSum, path);
            sum -= root.right.val;
            path.remove(path.size() - 1);
        }
    }
}

对于回溯函数需不需要返回值 

总结

再来看返回值,递归函数什么时候需要返回值?什么时候不需要返回值?这里总结如下三点:

  • 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。(这种情况就是本文下半部分介绍的113.路径总和ii)
  • 如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值。 (这种情况我们在236. 二叉树的最近公共祖先 (opens new window)中介绍)
  • 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。(本题的情况)

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

 按照转换的思路

主要是要确定边界 遵循左闭右开

class Solution {
    Map<Integer, Integer> map = null;
    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);
    }

    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;
    }
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值