代码随想三刷二叉树篇3

404. 左叶子之和

题目

链接

代码

/**
 * 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 int sumOfLeftLeaves(TreeNode root) {
        traverse(root,false);
        return sum;
    }
    int sum = 0;
    public void traverse(TreeNode root,boolean isLeft){
        if(root==null){
            return;
        }
        if(root.left==null&&root.right==null&&isLeft){
            sum+=root.val;
            return;
        }
        traverse(root.left,true);
        traverse(root.right,false);
    }
}

513. 找树左下角的值

题目

链接

代码

/**
 * 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 int findBottomLeftValue(TreeNode root) {
        Deque<TreeNode> queue = new LinkedList();
        queue.add(root);
        int result = root.val;
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i =0;i<size;i++){
                TreeNode temp = queue.removeFirst();
                if(i==0){
                    result = temp.val;
                }

                if(temp.left!=null){
                    queue.addLast(temp.left);
                }
                if(temp.right!=null){
                    queue.addLast(temp.right);
                }
            }
        }
        return result;
    }
}

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;
        }
        sum += root.val;
        traverse(root, targetSum);
        return isHas;
    }

    boolean isHas = false;
    int sum = 0;

    public void traverse(TreeNode root, int targetSum) {
        if (isHas) {
            return;
        }
        if (root == null) {
            return;
        }
        if (root.left == null && root.right == null) {
            if (targetSum == sum) {
                isHas = true;
                return;
            }
        }

        if (root.left != null) {
            sum += root.left.val;
            traverse(root.left, targetSum);
            sum -= root.left.val;
        }
        if (root.right != null) {
            sum += root.right.val;
            traverse(root.right, targetSum);
            sum -= root.right.val;
        }

    }
}

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

题目

链接

代码

/**
 * 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 TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder==null||inorder.length==0){
            return null;
        }
        if(inorder.length==1){
            return new TreeNode(inorder[0]);
        }

        int val = postorder[postorder.length-1];

        int index = 0;
        for(index = 0;index<inorder.length;index++){
            if(inorder[index]==val){
                break;
            }
        }
        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,inorder.length-1);
        TreeNode left = buildTree(leftinorder,leftpostorder);
        TreeNode right = buildTree(rightinorder,rightpostorder);
        TreeNode root =new TreeNode(val);
        root.left = left;
        root.right = right;
        return root;
    }   
}

654. 最大二叉树

题目

链接

代码

/**
 * 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 TreeNode constructMaximumBinaryTree(int[] nums) {
        return traversal(nums);
    }

    public TreeNode traversal(int [] nums){
        if(nums==null||nums.length==0){
            return null;
        }
        //叶子
        if(nums.length==1){
            return  new TreeNode(nums[0]);
        }
        //找最大的
        int max = nums[0];
        int maxIndex = 0;
        for(int i=1;i<nums.length;i++){
            if(max<nums[i]){
                max = nums[i];
                maxIndex = i;
            }
        }
        TreeNode t = new TreeNode(max);
        //分割左右
        int[] leftNums = Arrays.copyOfRange(nums,0,maxIndex);
        int[] rightNums = Arrays.copyOfRange(nums,maxIndex+1,nums.length);
        t.left = traversal(leftNums);
        t.right = traversal(rightNums);
        return t;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值