2015年10月10日星期六

#LeetCode# Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

这道题很简单,递归无脑。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public int minDepth(TreeNode root) {
        
        if(root == null) 
            return 0;
        if(root.left == null) 
            return minDepth(root.right) + 1;
            
        if(root.right == null) 
            return minDepth(root.left) + 1;
        
        return Math.min(
            minDepth(root.right), minDepth(root.left)) + 1;
        
    }
}

#LeetCode# Path Sum

Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.
For example:
Given the below binary tree and sum = 22,
              5
             / \
            4   8
           /   / \
          11  13  4
         /  \      \
        7    2      1
return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22.

这道题得逆向思维。

之前考虑的一直都是,怎么从根到叶子的结点权值挨个相加,然后与sum值比较。这样就得考虑叶子个数的数的处理,如何存储或者输出?用递归的话该如何实现?怎么分清楚左右子树。头大。

换个思路,将sum自根节点起逐步减结点权值,最后与叶子的权值比较,这样就好处理多了!!
核心代码就一句,return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);

上代码。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean hasPathSum(TreeNode root, int sum) {
        
        if(root == null)
            return false;
            
        if(root.val == sum && root.left == null && root.right == null)
            return true;
        
        return hasPathSum(root.left, sum - root.val) || hasPathSum(root.right, sum - root.val);
    }
}

2015年10月7日星期三

#LeetCode# Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
Follow up:
Could you do it without any loop/recursion in O(1) runtime?

对给定的数逐位求和,直至得到的结果是个位数。以前处理过逐位求和,无非是sum=sum+num%10, num=num/10。

这里我们加上判断结果是否为个位数的判定条件,然后递归调用即可。

上代码,

public class Solution {
    public int addDigits(int num) {
        num = sum(num);
        while(num > 9) {
            num = sum(num);
        }
        return num;
    }
    
    private int sum(int num) {
        int sum = 0;
        while (num >0) {
        sum = sum + num % 10;
        num = num/10;
        }
        return sum;
    }
}

此方法的时间复杂度是多少呢?

如何用O(1)求解,这是个问题。

#LeetCode# Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree is symmetric:
    1
   / \
  2   2
 / \ / \
3  4 4  3
But the following is not:
    1
   / \
  2   2
   \   \
   3    3
Note:
Bonus points if you could solve it both recursively and iteratively.
confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

去年用OCaml学算法的时候做过这题,清楚记得当初法语描述是mirror arbre。这次用java做,遇到几个问题,

1. 不仅要考虑左右两子树同时为Null的情况,还要考虑两者不同时为Null的情况
2. 镜面树,是严格对称。第一次写成了left.left==right.left && left.right==right.right 结果Fail我还纳闷了半天

好了,上代码,

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null) {
            return true;
        }
        
        return isEqual(root.left, root.right);
        
    }
    
    private boolean isEqual (TreeNode left, TreeNode right) {
        
        if(left == null && right == null) {
            return true;
        }
        
        if(left == null || right ==null) {
            return false;
        }
        
        return left.val == right.val && isEqual(left.left, right.right) && isEqual(left.right, right.left);
    
    }

}

平时的课好好学,还是很有用的。M1的目标就是好好学算法和数学。加油!