代码随想录算法训练营Day16|513、找树左下角的值 、112、路径总和、106、从中序后序构造二叉树

513、找树左下角的值

        思路:题目意思时最深的最左。用层序会更简单。这里练习下递归

        代码框架:先定义两个全局变量来记录深度和val值。一开始的深度设置为-1,这样传递参数进去时才会更新val值。

函数头:返回值是空,因为用全局变量记录了值。传递的参数是root和depth。        

递归终止条件:当为叶子节点,并且深度最大时,更新depth 和value。

单个循环体:遍历左右节点,这里一定先遍历左边节点,这样右边节点即使相同深度也不会更新了。

/**
 * 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 {    
    int maxDepth = -1;
    int maxValue =  0;
    public int findBottomLeftValue(TreeNode root) {

        travalsal(root, 0);
        return maxValue;
    }
    public void travalsal(TreeNode root, int Depth){
        if(root.left == null && root.right == null && Depth > maxDepth){
            maxDepth = Depth;
            maxValue = root.val;
        }
        if(root.left !=null){
            Depth++;
            travalsal(root.left,Depth);
            Depth--;
        }
        if(root.right !=null){
            Depth++;
            travalsal(root.right, Depth);
            Depth--;
        }
    }
}

 112.路径总和

/**
 * 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;
        return ISEqual(root,targetSum);
    }

    public boolean ISEqual(TreeNode root, int count){
        count -= root.val;
        if(root.left == null && root.right == null){
            if(count == 0){
                return true;
            }else{
                return false;
            }
        }
        if(root.left != null){
            boolean flag = ISEqual(root.left, count);
            if(flag == true) return true;
        }
        if(root.right != null){
            boolean flag = ISEqual(root.right,count);
            if(flag == true) return true;
        }
        return false;
    }
}

113、路径总和2

/**
 * 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;
        }
        List<Integer> nums = new ArrayList<>();
        findPath(root, targetSum, nums, res);
        return res;
    }

    public void findPath(TreeNode root, int count, List<Integer> nums, List<List<Integer>> res){
        nums.add(root.val);
        if(root.left == null && root.right == null){
            if(count -root.val == 0){
                res.add(new ArrayList<>(nums));
            }
            return;
        }
        if(root.left != null){
            //nums.add(root.left.val);
            findPath(root.left, count - root.val, nums, res);
            nums.remove(nums.size()-1);
        }
        if(root.right != null){
            //nums.add(root.right.val);
            findPath(root.right, count - root.val, nums, res);
            nums.remove(nums.size()-1);

        }
        return;
    }
}

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

        思路:和数据结构教材的一致,主要归纳下代码框架

        代码框架:1、空节点,返回null。(后序数组长度为0)

2、取出最后一个元素,放入根节点

3、如果数组大小为1,返回

4、找到跟节点在中序数组中的索引

5、切割中序数组

6、根据切割出来的左右区间,切割后序数组

7、递归处理左右子区间

/*
int[] sourceArr = {1, 2, 3, 4, 5, 6};
int startIndex = 2;
int endIndex = 4;
int[] subArray = Arrays.copyOfRange(sourceArr, startIndex, endIndex + 1);
*/
class Solution {
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(postorder.length == 0){
            return null;
        }
        int rootValue = postorder[postorder.length - 1];
        TreeNode root = new TreeNode(rootValue);
        if(postorder.length == 1) return root;
        int index = 0;
        for(int i = 0; i < inorder.length; i++){
            if(inorder[i] == rootValue){
                index = i;
            }
        }
        int[] leftInorder = Arrays.copyOfRange(inorder,0,index);
        int[] rightInorder = Arrays.copyOfRange(inorder,index+1,inorder.length);
        int[] leftPostorder = Arrays.copyOfRange(postorder, 0, index);
        int[] rightPostorder = Arrays.copyOfRange(postorder, index, postorder.length -1);
        root.left = buildTree(leftInorder, leftPostorder);
        root.right = buildTree(rightInorder,rightPostorder);
        return root;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值