java算法题每日多道七

104. 二叉树的最大深度

题目

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

输入:root = [3,9,20,null,null,15,7]
输出:3

示例 2:

输入:root = [1,null,2]
输出:2

答案

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        int left = maxDepth(root.left);
        int right = maxDepth(root.right);
        return Math.max(left,right) + 1;
    }
}

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        int res = 0;
        while(!queue.isEmpty()){
            int size = queue.size();
            res++;
            for(int i=0;i<size;i++){
                TreeNode curr = queue.poll();
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
        }
        return res;
    }
}








100. 相同的树

题目

给你两棵二叉树的根节点 pq ,编写一个函数来检验这两棵树是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

答案

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null && q==null){
            return true;
        }
        //根左右
        if(p==null || q==null){
            return false;
        }
        if(p.val!= q.val){
            return false;
        }
        boolean left = isSameTree(p.left,q.left);
        boolean right = isSameTree(p.right,q.right);
        return left && right;
    }
}

class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if(p==null && q==null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(q);
        queue.offer(p);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i=0;i<size;i++){
                TreeNode left = queue.poll();
                TreeNode right = queue.poll();
                if(left==null && right==null){
                    continue;
                }
                if(left==null || right==null){
                    return false;
                }
                if(left.val!=right.val){
                    return false;
                }
                queue.offer(left.left);
                queue.offer(right.left);
                queue.offer(left.right);
                queue.offer(right.right);
            }
        }
        return true;
    }
}









226. 翻转二叉树

题目

给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。

答案

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        //左右根
        root.left = invertTree(root.left);
        root.right = invertTree(root.right);
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
        return root;
    }
}

class Solution {
    public TreeNode invertTree(TreeNode root) {
        if(root==null){
            return null;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i=0;i<size;i++){
                TreeNode curr = queue.poll();
                deal(curr);
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
        }
        return root;
    }
    void deal(TreeNode root){
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
    }
}









101. 对称二叉树

题目

给你一个二叉树的根节点 root , 检查它是否轴对称

答案

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        return deal(root.left,root.right); 
    }
    boolean deal(TreeNode p,TreeNode q){
        if(p==null && q==null){
            return true;
        }
        //根左右
        if(p==null || q==null){
            return false;
        }
        if(p.val != q.val){
            return false;
        }
        boolean left = deal(p.left,q.right);
        boolean right = deal(p.right,q.left);
        return left && right;
    }
}

class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root==null){
            return true;
        }
        Queue<TreeNode> queue = new LinkedList();
        queue.offer(root.left);
        queue.offer(root.right);
        while(!queue.isEmpty()){
            int size = queue.size();
            for(int i=0;i<size;i++){
                TreeNode left = queue.poll();
                TreeNode right = queue.poll();
                if(left==null && right==null){
                    continue;
                }
                if(left==null || right==null){
                    return false;
                }
                if(left.val != right.val){
                    return false;
                }
                queue.offer(left.left);
                queue.offer(right.right);
                queue.offer(left.right);
                queue.offer(right.left);
            }
        }
        return true;
    }
}









105. 从前序与中序遍历序列构造二叉树

题目

给定两个整数数组 preorderinorder ,其中 preorder 是二叉树的先序遍历inorder 是同一棵树的中序遍历,请构造二叉树并返回其根节点。

答案

class Solution {
    Map<Integer,Integer> map;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        map = new HashMap();
        for(int i=0;i<inorder.length;i++){
            map.put(inorder[i],i);
        }
        TreeNode root = deal(inorder,0,inorder.length,preorder,0,preorder.length);
        return root;
    }
    TreeNode deal(int[] inorder,int inBegin,int inEnd,int[] preorder,int preBegin,int preEnd){
        if(inBegin>=inEnd && preBegin>=preEnd){
            return null;
        }
        int rootVal = preorder[preBegin];
        int rootIndex = map.get(rootVal);
        int leftLen = rootIndex - inBegin;
        TreeNode root = new TreeNode(rootVal);
        root.left = deal(inorder,inBegin,rootIndex,preorder,preBegin+1,preBegin+1+leftLen);
        root.right =deal(inorder,rootIndex+1,inEnd,preorder,preBegin+1+leftLen,preEnd);
        return root;
    }
}









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

题目

给定两个整数数组 inorderpostorder ,其中 inorder 是二叉树的中序遍历, postorder 是同一棵树的后序遍历,请你构造并返回这颗 二叉树

答案

class Solution {
    Map<Integer,Integer> map;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        map = new HashMap();
        for(int i=0;i<inorder.length;i++){
            map.put(inorder[i],i);
        }
        TreeNode root = deal(inorder,0,inorder.length,postorder,0,postorder.length);
        return root;
    }
    TreeNode deal(int[] inorder,int inBegin,int inEnd,int[] postorder,int postBegin,int postEnd){
        if(inBegin>=inEnd && postBegin>=postBegin){
            return null;
        }
        int rootVal = postorder[postEnd-1];
        int rootIndex = map.get(rootVal);
        int leftLen = rootIndex - inBegin;
        TreeNode root = new TreeNode(rootVal);
        root.left = deal(inorder,inBegin,rootIndex,postorder,postBegin,postBegin+leftLen);
        root.right = deal(inorder,rootIndex+1,inEnd,postorder,postBegin+leftLen,postEnd-1);
        return root;
    }
}









117. 填充每个节点的下一个右侧节点指针 II

题目

给定一个二叉树:

struct Node {
  int val;
  Node *left;
  Node *right;
  Node *next;
}

填充它的每个 next 指针,让这个指针指向其下一个右侧节点。如果找不到下一个右侧节点,则将 next 指针设置为 NULL

初始状态下,所有 next 指针都被设置为 NULL

答案

class Solution {
    public Node connect(Node root) {
        if(root==null){
            return null;
        }
        Queue<Node> queue = new LinkedList();
        queue.offer(root);
        while(!queue.isEmpty()){
            int size = queue.size();
            Node pre = queue.poll();
            if(pre.left!=null) queue.offer(pre.left);
            if(pre.right!=null) queue.offer(pre.right);
            for(int i=1;i<size;i++){
                Node curr = queue.poll();
                pre.next = curr;//前一个指向当前节点
                pre = curr;
                if(curr.left!=null) queue.offer(curr.left);
                if(curr.right!=null) queue.offer(curr.right);
            }
        }
        return root;
    }
}









114. 二叉树展开为链表

题目

给你二叉树的根结点 root ,请你将它展开为一个单链表:

  • 展开后的单链表应该同样使用 TreeNode ,其中 right 子指针指向链表中下一个结点,而左子指针始终为 null
  • 展开后的单链表应该与二叉树 先序遍历 顺序相同。

答案

class Solution {
    List<TreeNode> list;
    public void flatten(TreeNode root) {
        if(root==null){
            return;
        }
        list = new ArrayList();
        deal(root);
        TreeNode pre = list.get(0);
        for(int i=1;i<list.size();i++){
            TreeNode curr = list.get(i);
            pre.left = null;
            pre.right = curr;
            pre = curr;
        }
    }
    void deal(TreeNode root){
        if(root==null){
            return;
        }
        list.add(root);
        deal(root.left);
        deal(root.right);
    }
}









112. 路径总和

题目

给你二叉树的根节点 root 和一个表示目标和的整数 targetSum 。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum 。如果存在,返回 true ;否则,返回 false

叶子节点 是指没有子节点的节点。

答案

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root==null){
            return false;
        }
        //根左右
        if(root.left==null && root.right==null && targetSum==root.val){
            return true;
        }
        boolean left = hasPathSum(root.left,targetSum-root.val);
        boolean right = hasPathSum(root.right,targetSum-root.val);
        return left || right;
    }
}
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值