671. 二叉树中第二小的节点 671. Second Minimum Node In a Binary Tree 题目描述 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中的第二小的值.如果第二小的值不存在的话,输出 -1. 每日一算法2019/5/12Day 9LeetCode671. Second Minimum Node In a Bin…
二叉树中第二小的节点 给定一个非空特殊的二叉树,每个节点都是正数,并且每个节点的子节点数量只能为 2 或 0.如果一个节点有两个子节点的话,那么这个节点的值不大于它的子节点的值. 给出这样的一个二叉树,你需要输出所有节点中的第二小的值.如果第二小的值不存在的话,输出 -1 . 思路 class Solution { int min1; long ans = Long.MAX_VALUE; public void dfs(TreeNode root) { if (root != null) { i…
Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and doe…
1.在project中,相对路径的根目录是project的根文件夹,在此就是repathtest文件夹了.创建文件的写法是: File f = new File("src/com/lavasoft/res/a.txt"); File f = new File("doc/b.txt"); 注意:路径不以"/"开头: 脱离了IDE环境,这个写法就是错误的,也并非每个IDE都如此. 2.通过CLASSPATH读取包内文件 读取包内文件,使用的路径一定是…
题目: Given a non-empty binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path must contain at least one node and…
题目: 给定一个二叉搜索树,编写一个函数 kthSmallest 来查找其中第 k 个最小的元素. 说明:你可以假设 k 总是有效的,1 ≤ k ≤ 二叉搜索树元素个数. 这道题在leetCode上难度为中等,但我觉得其实只要掌握了二叉搜索树遍历的特点就很好解决了.二叉搜索树的中序遍历顺序是由低到高的升序,所以我们只需要找到中序遍历第k个遍历出来的就行了. 代码: class Solution { private int i = 0; private int val = 0; public vo…
387. 字符串中的第一个唯一字符 给定一个字符串,找到它的第一个不重复的字符,并返回它的索引.如果不存在,则返回 -1. 案例: s = "leetcode" 返回 0. s = "loveleetcode", 返回 2. 注意事项:您可以假定该字符串只包含小写字母. class Solution { public int firstUniqChar(String s) { //fast int n = s.length(); for(int i = 'a'; i…
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Return 6. 解题思路: DFS暴力枚举,注意,如果采用static 全局变量的话,在IDE里面是可以通过,但在OJ上无法测试通过,因此需要建立一个类来储存结果,JAVA实现如下: publ…
833. 字符串中的查找与替换 对于某些字符串 S,我们将执行一些替换操作,用新的字母组替换原有的字母组(不一定大小相同). 每个替换操作具有 3 个参数:起始索引 i,源字 x 和目标字 y.规则是如果 x 从原始字符串 S 中的位置 i 开始,那么我们将用 y 替换出现的 x.如果没有,我们什么都不做. 举个例子,如果我们有 S = "abcd" 并且我们有一些替换操作 i = 2,x = "cd",y = "ffff",那么因为 "…