leetCode解题报告之5道基础题II(一)

2014-11-24 11:34:43 · 作者: · 浏览: 2

leetcode上比较简单的几道题,不想分开写几篇博文来记录哈,所以写在一起了!!

虽然几道题都比较简单,但感觉自己写得不好,希望博文如果被哪位大神看到,可以留言下写下你的做法!

题目一:

Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

题意:给你一个二叉树的根结点,求出树的深度!(递归)


/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int maxDepth(TreeNode root) {
        return findMaxDepth(root);
    }
    
    public int findMaxDepth(TreeNode node){
        if (node == null)   //结点为null,返回0
            return 0;
        if (node.left == null && node.right == null)   //如果为叶子结点:返回深度1
            return 1;
        int leftDepth = findMaxDepth(node.left);    //求出左子树的最大深度
        int rightDepth = findMaxDepth(node.right);  //求出右子树的最大深度
        return leftDepth > rightDepth   leftDepth+1 : rightDepth+1; //比较左右子树的深度,取最大值+1 得出整个子树的深度
    }
}


题目二:

Sum Root to Leaf Numbers

Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.

An example is the root-to-leaf path 1->2->3 which represents the number 123.

Find the total sum of all root-to-leaf numbers.

For example,

    1
   / \
  2   3

The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.

Return the sum = 12 + 13 = 25.

题意:给你一棵二叉树,请你输出所有路径的和,路径上的结点的值连接起来组成这条路径的值(简单的递归算法)


/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    
    public int calSum(TreeNode root, int sum){
        if (root == null)
            return sum;
        if (root.left == null && root.right == null) //循环结束条件
            return sum * 10 + root.val;
        int leftChildSum = 0;
        int rightChildSum = 0;
        if (root.left != null){	//递归调用
            leftChildSum = calSum(root.left, sum * 10 + root.val);
        }
        if (root.right != null){//递归调用
            rightChildSum = calSum(root.right, sum * 10 + root.val);
        }
        
        return (leftChildSum + rightChildSum);
    }
    
    public int sumNumbers(TreeNode root) {
        if (root == null)
            return 0;
        return calSum(root, 0);
    }
}

题目三:

Unique Binary Search Trees

Given n, how many structurally unique BST's (binary search trees) that store values 1...n

For example,
Given n = 3, there are a total of 5 unique BST's.

   1         3     3      2      1
    \       /     /      / \      \
     3     2     1      1   3      2
    /     /       \                 \
   2     1         2                 3

题意:给你一个数n, 这个n表示二叉树拥有1,2,....,n这n个节点,让你去求不相等的又满足BST(二叉搜索树)特性的二叉树有多少种情况。

分析:递归问题哈

这道题是递归的意思在这里面
* 情况:
* n=1 : 只有一种可能,则返回1
* n=2 : 两种可能返回2;
* n=3 : 返回5;
* n>3 : 我们知道,如果一个问题特别大,我们会想办法把它分解成小问题来解决
* 我们取特殊情况n=4来做下分析
* 如果n=4, 二叉树根结点的值可以是:1,2,3,4这四种情况
*
* 设root.val 用 i来表示, for循环 i < n, 左子树上的结点数量: i-1, 右子树上的结点数量: n-i
*
* 而当i=1的时候,左子树结点Number: i-1=0 ,右子树结点Number: n-i=4-1=3
* 此时这种情况得到的二叉树个数便转换成了求右边3个结点拥有的情况数量(递归)
*
* 而当i = 2的时候,左子树结点Number: i-1=1 ,右子树结点Number: n-i=4-2=2
* 此时这种情况得到的二叉树个数便转换成了求左边情况的个数*右边情况的个数(组合数学)
* [当然如果左边或者右边有一方为0的话,就返回一支子树的数量就行了]
*
* 而当i = 3的时候,左子树结点Number: i-1=2 ,右子树结点Number: n-i=4-3=1
* 此时这种情况得到的二叉树个数便转换成了求左边情况的个数*右边情况的个数(组合数学)
* [当然如果左边或者右边有一方为0的话,就返回一支子树的数量就行了]
*
* 而当i = 4的时候,左子树结点Number: i-1=3 ,右子树结点Number: n-i=4-4=0
* 此时这种情况得到的二叉树个数便转换成了求左边3个结点拥有的情况数量(递归)
*
* 最终所有情况加起来就是 n = 4的所有二叉树数目


AC代码:

package copylist;

/**
 * 
 * @Description:  [leetcode Unique Binary Search Trees]
 * @Author:       [胖虎]   
 * @CreateDate:   [2014-4-3 下午8:19:44]
 * @CsdnUrl:      [http://blog.csdn.net/ljphhj]
 */
public class Solution {
    public int numTrees(int n) {
        
        if (n == 0 || n == 1)
            return n;
            
        return calSum(n);
    }
    public int calSum(int n){
        int sum = 0;