代码随想录算法训练营Day16|LeetCode513找树左下角的值、LeetCode112路径总和、LeetCode113路径总和III、LeetCode106从中序与后序遍历序列构造二叉树

Day16

LeetCode513 找树左下角的值

找树左下角的值

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

//递归法
class Solution {
    private int Depth = -1;
    private int value = 0;
    public int findBottomLeftValue(TreeNode root) {
        value = root.val;
        findLeftValue(root,0);
        return value;
    }

    private void findLeftValue (TreeNode root,int depth) {
        if (root == null) return;
        if (root.left == null && root.right == null) {
            if (depth > Depth) {
                value = root.val;
                Depth = depth;
            }
        }
        if (root.left != null) findLeftValue(root.left,depth + 1);
        if (root.right != null) findLeftValue(root.right,depth + 1);
    }
}

//迭代法
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        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;
    }

}

LeetCode112 路径总和

路径总和

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

//递归法|
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;
        if(root.left == null && root.right == null) return root.val == targetSum;
        return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val);
    }
}

//迭代法
class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null) return false;
        
        Deque<TreeNode> stack = new LinkedList<>();
        Deque<Integer> sumStack = new LinkedList<>();
        stack.offer(root);
        sumStack.push(root.val);
        while(!stack.isEmpty()){
            TreeNode topNode = stack.peek();
            Integer topSum = sumStack.pop();
            if(topNode != null){
                stack.push(null);
                sumStack.push(topSum);
                if(topNode.right != null) {
                    stack.push(topNode.right);
                    sumStack.push(topSum + topNode.right.val);
                }
                if(topNode.left != null) {
                    stack.push(topNode.left);
                    sumStack.push(topSum + topNode.left.val);
                }
            }else{
                stack.pop();
                topNode = stack.pop();
                if(topNode.left == null && topNode.right == null && topSum == targetSum) return true; 
            }
        }
        return false;
    }
}

LeetCode113 路径总和II

路径总和II

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */

//递归法
class Solution {
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;
        traversal(res, new LinkedList<Integer>(), root, targetSum);
        return res;
    }

    private void traversal(List<List<Integer>> res, List<Integer> path, TreeNode root, int targetSum){
        path.add(root.val);
        if(root.left == null && root.right == null && root.val == targetSum) 
              res.add(new ArrayList<Integer>(path));
        else{
            if(root.left != null){
                traversal(res, path, root.left, targetSum - root.val);
                path.remove(path.size() - 1);
            }
            if(root.right != null){
                traversal(res, path, root.right, targetSum - root.val);
                path.remove(path.size() - 1);
            }
        }      
    }
}

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

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

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    Map<Integer, Integer> inorderIndex = new HashMap<>();
    public TreeNode buildTree(int[] inorder, int[] postorder) {
       for(int i = 0; i < inorder.length; i++) inorderIndex.put(inorder[i], i); 
       return buildTree(inorder, postorder, 0, inorder.length - 1, 0, postorder.length - 1);   
    }

    private TreeNode buildTree(int[] inorder, int[] postorder, int inStart, int inEnd, int postStart, int postEnd){
         if(inStart > inEnd || postStart > postEnd) return null; 

         int rootVal = postorder[postEnd];         
         int leftInEnd = inorderIndex.get(rootVal) - 1;

         TreeNode root = new TreeNode(rootVal);
         int diff = leftInEnd - inStart;
         root.left = buildTree(inorder, postorder, inStart, leftInEnd, postStart, postStart + diff);
         root.right = buildTree(inorder, postorder, leftInEnd + 2, inEnd, postStart + diff + 1, postEnd - 1);
        return root;

    }
}

ldTree(inorder, postorder, inStart, leftInEnd, postStart, postStart + diff);
root.right = buildTree(inorder, postorder, leftInEnd + 2, inEnd, postStart + diff + 1, postEnd - 1);
return root;

}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值