Minimum Depth of Binary Tree


public class Solution {
public int minDepth(TreeNode root) {
if(root==null) return 0;
int lh = minDepth(root.left);
int rh = minDepth(root.right);
return (lh==0 || rh==0)?lh+rh+1:Math.min(lh,rh)+1;
}
}

Maxmum Depth of Binary Tree


class Solution {
public:
int dep = 0;
int maxDepth(TreeNode* root) {
dep = dfs(root);
return dep;
}
int dfs(TreeNode* root){
if(root == NULL) return 0;
int left = dfs(root->left);
int right = dfs(root->right);
return 1+max(left,right);
}
};

Binary Tree Maximum Path Sum


public class Solution {
int maxValue;
public int maxPathSum(TreeNode root) {
maxValue = Integer.MIN_VALUE;
maxPathDown(root);
return maxValue;
}
private int maxPathDown(TreeNode node) {
if (node == null) return 0;
int left = Math.max(0, maxPathDown(node.left));
int right = Math.max(0, maxPathDown(node.right));
maxValue = Math.max(maxValue, left + right + node.val);
return Math.max(left, right) + node.val;
}
}

Sum Root to Leaf Numbers


public class Solution {
public int sumNumbers(TreeNode root) {
return sum(root, 0);
}
public int sum(TreeNode n, int s){
if (n == null) return 0;
if (n.right == null && n.left == null) return s*10 + n.val;
return sum(n.left, s*10 + n.val) + sum(n.right, s*10 + n.val);
}
}

Binary Tree Paths


public class Solution {
public List<String> binaryTreePaths(TreeNode root) {
List<String> rst = new ArrayList<String>();
if(root == null) return rst;
StringBuilder sb = new StringBuilder();
helper(rst, sb, root);
return rst;
} public void helper(List<String> rst, StringBuilder sb, TreeNode root){
if(root == null) return;
int tmp = sb.length();
if(root.left == null && root.right == null){
sb.append(root.val);
rst.add(sb.toString());
sb.delete(tmp , sb.length());
return;
}
sb.append(root.val + "->");
helper(rst, sb, root.left);
helper(rst, sb, root.right);
sb.delete(tmp , sb.length());
return; }
}

Binary Tree Right Side View


public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> res = new ArrayList<Integer>();
if(root == null) return res;
LinkedList<TreeNode> q = new LinkedList<TreeNode>();
q.addLast(root);
while(!q.isEmpty()){
int tmp = q.size();
TreeNode tmpNode ;
for(int i=1;i<=tmp;i++){
tmpNode = q.getFirst();
q.removeFirst();
if(i == tmp){
res.add(tmpNode.val);
}
if(tmpNode.left!=null) q.addLast(tmpNode.left);
if(tmpNode.right!=null) q.addLast(tmpNode.right);
}
}
return res;
}
}

Path Sum I


public class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
if (sum - root.val == 0 && root.left == null && root.right == null) {
return true;
}
return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
}
}

Path Sum II


public class Solution {
public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<List<Integer>>();
List<Integer> cur = new ArrayList<Integer>();
help(root,sum,0,res,cur);
return res;
}
public void help(TreeNode p, int sum,int curSum,List<List<Integer>> res,List<Integer> curList){
if(p==null) return;
curList.add(p.val);
if(curSum+p.val == sum && p.left==null && p.right==null){
res.add(new ArrayList(curList));
curList.remove(curList.size()-1);
return;
}else{
help(p.left,sum,curSum+p.val,res,curList);
help(p.right,sum,curSum+p.val,res,curList);
}
curList.remove(curList.size()-1);
return;
}
}

Kth Smallest Element in a BST


public class Solution {
public int kthSmallest(TreeNode root, int k) {
LinkedList<TreeNode> s = new LinkedList<TreeNode>();
int cnt = 0;
TreeNode p = root;
while(!s.isEmpty() || p!=null){
if(p!=null){
s.addFirst(p);
p = p.left;
}else{
p = s.getFirst();
s.removeFirst();
cnt++;
if(cnt==k){return p.val;}
p = p.right;
}
}
return 0;
}
}

Unique Binary Search Trees


public class Solution {
public int numTrees(int n) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(0,1);
map.put(1,1);
return numTrees(n, map);
} private int numTrees(int n, Map<Integer, Integer> map){
// check memory
if(map.containsKey(n)) return map.get(n);
// recursion
int sum = 0;
for(int i = 1;i <= n;i++)
sum += numTrees(i-1, map) * numTrees(n-i, map);
map.put(n, sum);
return sum;
}
}

Unique Binary Search Trees II


public class Solution {
public List<TreeNode> generateTrees(int n) {
// DP, compute and store trees for intermediate ranges from 1 to n
// time: exponential, since for each range the number of trees is combinatorial
// space: O(n^2)
if (n == 0) {
List<TreeNode> result = new ArrayList<TreeNode>();
//result.add(null);
return result;
}
List<TreeNode>[][] treesForRange = (ArrayList<TreeNode>[][]) new ArrayList[n+2][n+1];
for (int step = 0; step < n; step++) {
for (int i = 1; i <= n - step; i++) {
int j = i + step;
treesForRange[i][j] = new ArrayList<TreeNode>();
if (step == 0) {
treesForRange[i][j].add(new TreeNode(i));
continue;
}
// k is a possible root for range i-j
for (int k = i; k <= j; k++) {
// two ugly if blocks for boundary cases:
// when k == i, the left subtree is null
// when k == j, the right subtree is null
if (treesForRange[i][k-1] == null) {
treesForRange[i][k-1] = new ArrayList<TreeNode>();
treesForRange[i][k-1].add(null);
}
if (treesForRange[k+1][j] == null) {
treesForRange[k+1][j] = new ArrayList<TreeNode>();
treesForRange[k+1][j].add(null);
}
for (TreeNode leftNode : treesForRange[i][k-1]) {
for (TreeNode rightNode : treesForRange[k+1][j]) {
TreeNode root = new TreeNode(k);
root.left = leftNode;
root.right = rightNode;
treesForRange[i][j].add(root);
}
}
}
}
}
return treesForRange[1][n];
}
}

LeetCode(五)的更多相关文章

  1. LeetCode第五天

    leetcode 第五天 2018年1月6日 22.(566) Reshape the Matrix JAVA class Solution { public int[][] matrixReshap ...

  2. LeetCode算法题-Number Complement(Java实现-五种解法)

    这是悦乐书的第240次更新,第253篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第107题(顺位题号是476).给定正整数,输出其补码数.补充策略是翻转其二进制表示的位 ...

  3. LeetCode算法题-Longest Palindrome(五种解法)

    这是悦乐书的第220次更新,第232篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第87题(顺位题号是409).给定一个由小写或大写字母组成的字符串,找到可以用这些字母构 ...

  4. LeetCode算法题-Find the Difference(Java实现-五种解法)

    这是悦乐书的第214次更新,第227篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第82题(顺位题号是389).给定两个字符串s和t,它们只包含小写字母.字符串t由随机混 ...

  5. leetcode算法刷题(五)——动态规划(三)

    今天的题目不是leetcode上面的.只是觉得动态规划还是不算很熟练,就接着找了点DP的题练练 最长递增子序列的长度 题目的意思:传入一个数组,要求出它的最长递增子序列的长度.例如:如在序列1,-1, ...

  6. leetcode#42 Trapping rain water的五种解法详解

    leetcode#42 Trapping rain water 这道题十分有意思,可以用很多方法做出来,每种方法的思想都值得让人细细体会. 42. Trapping Rain WaterGiven n ...

  7. LeetCode子域名访问计数-Python3.7<五>

    上一篇:LeetCode 键盘行<四> 题目:https://leetcode-cn.com/problems/subdomain-visit-count/description/ 一个网 ...

  8. LeetCode 笔记系列五 Generate Parentheses

    题目: Given n pairs of parentheses, write a function to generate all combinations of well-formed paren ...

  9. 最长回文子串 C++实现 java实现 leetcode系列(五)

    给定一个字符串 s,找到 s 中最长的回文子串.你可以假设 s 的最大长度为 1000. 示例 1: 输入: "babad" 输出: "bab" 注意: &qu ...

随机推荐

  1. WinForm中使用XML文件存储用户配置及操作本地Config配置文件

    大家都开发winform程序时候会大量用到配置App.config作为保持用户设置的基本信息,比如记住用户名,这样的弊端就是每个人一些个性化的设置每次更新程序的时候会被覆盖. 故将配置文件分两大类: ...

  2. java 网络编程复习(转)

    好久没有看过Java网络编程了,现在刚好公司有机会接触,顺便的拾起以前的东西 参照原博客:http://www.cnblogs.com/linzheng/archive/2011/01/23/1942 ...

  3. php_sapi_name详解

    php_sapi_name : — 返回 web 服务器和 PHP 之间的接口类型. 返回描述 PHP 所使用的接口类型(the Server API, SAPI)的小写字符串. 例如,CLI 的 P ...

  4. NET异常 在 getsockopt 或 setsockopt 调用中指定的一个未知的、无效的或不受支持的选项或层次。

    var Listener = new TcpListener(IPAddress.Any, port); Listener.AllowNatTraversal(true); // 在WIN8中调试没问 ...

  5. express - ejs使用介绍

    http://blog.sina.com.cn/s/blog_ad0672d60101l2ml.html 1.express中使用ejs var express = require('express' ...

  6. Python绑定方法,未绑定方法,类方法,实例方法,静态方法

    >>> class foo(): clssvar=[1,2] def __init__(self): self.instance=[1,2,3] def hehe(self): pr ...

  7. PaintCode调研

    1.   背景 PaintCode是一款面向iOS和Mac应用开发者及设计师的矢量图形可视化开发工具.它可以让设计师把设计好的psd文件直接导入该工具,然后生成用Quartz 2D 产生的object ...

  8. zhuang 定制iOS 7中的导航栏和状态栏

    近期,跟大多数开发者一样,我也正忙于对程序进行升级以适配iOS 7.最新的iOS 7外观上有大量的改动.从开发者的角度来看,导航栏和状态栏就发生了明显的变化.状态栏现在是半透明的了,这也就意味着导航栏 ...

  9. Flex 加载tiff

    gis系统常常要加载tiff,因为好多土地证书,各种文件都是扫描件,如果你是用as来写的前台,怎么加载呢,顺便说下用插件AlternaTIFF也是可以得不过浏览器加载这么多插件是不太好的. 首先TIF ...

  10. 【练习】数据移动---parfile导出表中指定行:

    要求: ①创建存放数据的文件: ②使用默认的bad文件生成方式: ③使用truncate选项方式. 1.准备条件: [oracle@host03 ~]$ mkdir datadump [oracle@ ...