LeetCode

100. 相同树

/**
 * 100. 相同的树
 */
public class IsSameTree {

    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p != null && q != null && p.val == q.val) {
            return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        }
        return false;
    }

    class TreeNode {
        int val;
        private TreeNode left;
        private TreeNode right;
        TreeNode(int val) {
            this.val = val;
        }
    }
}

101. 对称二叉树

/**
 * 101. 对称二叉树
 * 给定一个二叉树,检查它是否是镜像对称的。
 */
public class IsSymmetric {

    public boolean isSymmetric(TreeNode root) {
        return isMirror(root, root);
    }

    private boolean isMirror(TreeNode p, TreeNode q) {
        if (p == null && q == null) {
            return true;
        }
        if (p == null || q == null) {
            return false;
        }
        return p.val == q.val && isMirror(p.left, q.right) && isMirror(p.right, q.left);
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val) {
            this.val = val;
        }
    }
}

104. 二叉树的最大深度

/**
 * 104. 二叉树的最大深度
 */
public class MaxDepth {

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

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val) {
            this.val = val;
        }
    }
}

108. 将有序数组转化成二叉搜索树

/**
 * 108. 将有序数组转换为二叉搜索树
 */
public class SortedArrayToBST {

    public TreeNode sortedArrayToBST(int[] nums) {
        if (nums == null || nums.length == 0) {
            return null;
        }
        return this.buildTree(nums, 0, nums.length-1);
    }

    private TreeNode buildTree(int[] nums, int l, int r) {
        if (l > r) {
            return null;
        }
        if (l == r) {
            return new TreeNode(nums[l]);
        }
       int mid = (l + r)/2;
       TreeNode root = new TreeNode(nums[mid]);
       root.left = buildTree(nums, 0, mid -1);
       root.right = buildTree(nums, mid + 1, r);
       return root;
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val) {
            this.val = val;
        }
    }
}

111. 二叉树最小深度

/**
 * leetcode 111-二叉树最小深度
 * 最小深度定义为根节点到最近叶子节点的深度
 * https://blog.csdn.net/u013115610/article/details/71252441
 */
public class MinDepth {

    public int minDepth(TreeNode root) {
        int leftDepth = 0;
        int rightDepth = 0;
        if (root == null) {
            return 0;
        }
        if (root.left == null && root.right == null) {
            return 1;
        }
        if (root.left != null) {
            leftDepth = minDepth(root.left);
        }else {
            leftDepth = Integer.MAX_VALUE;
        }
        if (root.right != null) {
            rightDepth = minDepth(root.right);
        }else {
            rightDepth = Integer.MAX_VALUE;
        }
        return Math.min(leftDepth, rightDepth) + 1;



    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val) {
            this.val = val;
        }
    }
}

112. 路径总和

public class BinaryTreePaths {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<>();
        String s = "";
        paths(root, list, s);
        return list;

    }

    /**
     *
     * @param root TreeNode
     * @param list 存放所有路径
     * @param s 某条路径
     */
    private void paths(TreeNode root, List<String> list, String s) {
        if (root == null) {
            return;
        }
        s += root.val + " ";
        if (root.left == null && root.right == null) {
            list.add(s.trim().replace(" ", "->"));
        }
        if (root.left != null) {
            paths(root.left, list, s);
        }
        if (root.right != null) {
            paths(root.right, list, s);
        }
    }

    class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int val) {
            this.val = val;
        }
    }
}

559. N叉树的最大深度

package com.bill.algorithm.dfs;

import java.util.List;

/**
 * N 叉树的最大深度
 */
public class NTreeMaxDepth {

    public int nTreeMaxDepth(Node root) {
        if (root == null) {
            return 0;
        }
        int depth = 0;
        for (Node node : root.children) {
            depth = Math.max(depth, nTreeMaxDepth(node));
        }
        return depth + 1;
    }


    class Node {
        int val;
        List<Node> children;

        public Node(int val, List<Node> children) {
            this.val = val;
            this.children = children;
        }
    }

}

35. 搜索插入位置

/**
 * 给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。
 * 如果目标值不存在于数组中,返回它将会被按顺序插入的位置。
 * 你可以假设数组中无重复元素。
 */
public class SearchInsert {

    public int searchInsert(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            if (nums[i] >= target) {
                return i;
            }
        }
        return nums.length;
    }
}

69. x 的平方根

/**
 * 实现 int sqrt(int x) 函数。
 * 计算并返回 x 的平方根,其中 x 是非负整数。
 * 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
 */
public class MySqrt {


    public int mySqrt(int x) {
        int left = 1;
        int right = 46341; // <2^31-1 开方
        int mid = 0;
        if (x == 0) {
            return 0;
        }
        while (left <right) {
            if (mid == (left + right)/2) {
                break;
            }
            mid = (left + right) / 2;
            if (mid * mid == x) {
                break;
            }else if (mid * mid < x) {
                left = mid;
            }else {
                right = mid;
            }
        }
        return mid;
    }
}

349. 两个数组的交集

/**
 * 最简单的方案是用A数组的所有元素去匹配B数组的所有元素,如果两个数组的大小为n, 遍历的时间复杂度为O(n)。
 * 但是我们可以用hash函数或者hash表去解决问题。将数组A的hash 到hash表中。然后继续将数组B继续hash到hash表中,如果发生hash碰撞则统计+1,最后得出数组的交集。
 * 时间复杂度是hash所有元素的复杂度。
 */
public class InterSection {

    public int[] intersection(int[] nums1, int[] nums2) {
        Hashtable<Integer, Integer> ht = new Hashtable<Integer, Integer>();
        List<Integer> tmpList = new ArrayList<Integer>();
        for (int e : nums1) {
            ht.put(e, e);
        }
        for (int e : nums2) {
            if (ht.get(e) != null) {
                tmpList.add(e);
            }
        }
        int[] result = new int[tmpList.size()];
        int i = 0;
        for (Integer value : tmpList) {
            result[i] = value;
            i++;
        }
        return result;
    }
}

704. 二分查找

            int low = 0;
    int high = nums.length - 1;
    while (low <= high) {
        if (nums[(low + high)/2]  == target) {
            return (low + high) / 2;
        }else if (nums[(low + high) / 2] >= target) {
            high = (low + high) / 2 - 1;
        }else {
            low = (low + high) / 2 + 1;
        }
    }
    return -1;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值