求给定二叉树的最小深度。最小深度是指树的根结点到最近叶子结点的最短路径上结点的数量。
public class Solution {
public int run(TreeNode root) {
if(root==null)
{
return 0;
}
if(root.left==null&&root.right!=null)
{
return run(root.right)+1;
}
else if(root.right==null&&root.left!=null)
{
return run(root.left)+1;
}
else
{
return Math.min(run(root.left),run(root.right))+1;
}
}
}
计算逆波兰式(后缀表达式)的值
运算符仅包含"+","-","*“和”/",被操作数可能是整数或其他表达式
import java.util.Stack;
public class Solution {
/**
解决思路:遍历字符串数组,每碰见一个数就入栈,碰见一个运算符,拿出两个数出栈运算后在入栈,直到剩下最后一个数就是我们想要的
**/
public int evalRPN(String[] tokens) {
Stack<Integer> stack01 = new Stack();
for(int i=0;i<tokens.length;i++)
{
if(tokens[i].equals("+")||tokens[i].equals("-")||tokens[i].equals("*")||tokens[i].equals("/"))
{
int a = stack01.pop();
int b = stack01.pop();
if(tokens[i].equals("+"))
{
stack01.push (b+a);
}
else if(tokens[i].equals("-"))
{
stack01.push (b-a);
}
else if(tokens[i].equals("*"))
{
stack01.push (b*a);
}
else if(tokens[i].equals("/"))
{
stack01.push (b/a);
}
}
else {
stack01.push(Integer.valueOf((tokens[i])));
}
}
return stack01.pop();
}
}
对于给定的n个位于同一二维平面上的点,求最多能有多少个点位于同一直线上
Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
**/public class Solution {
public int maxPoints(Point[] points) {
if (points == null || points.length == 0){
return 0;
}
//一个点或者两个点直接返回值
if (points.length < 3){
return points.length;
}
//计算所有点的斜率与截距
int max = 2 ;
int samePoint;
int singlePoint;
for(int i=0;i<points.length;i++)
{
samePoint = 0;
for(int j=i+1;j<points.length;j++)
{
singlePoint = 1;
int DistanceX = points[j].x - points[i].x;
int DistanceY = points[j].y - points[i].y;
if(DistanceX==0&&DistanceY==0)
{
samePoint++;
}
else
{
singlePoint = 2;
for(int k=j+1;k<points.length;k++)
{
int DistanceX2 = points[k].x - points[i].x;
int DistanceY2 = points[k].y - points[i].y;
if(DistanceX2*DistanceY == DistanceY2*DistanceX)
{
singlePoint++;
}
}
}
if(max<samePoint+singlePoint)
{
max = samePoint+singlePoint;
}
}
}
return max;
}
}
在O(n log n)的时间内使用常数级空间复杂度对链表进行排序。
import java.util.*;
public class Solution {
public ListNode sortList(ListNode head) {
if(head == null)
{
return null;
}
LinkedList<ListNode> list = new LinkedList();
ListNode temp = head;
while(temp!=null)
{
list.add(temp);
temp = temp.next;
}
list.sort(new Comparator<ListNode>(){
public int compare(ListNode o1,ListNode o2)
{
//升序
return o1.val > o2.val ? 1 : (o1.val == o2.val ? 0 : -1);
}
});
ListNode newHead = new ListNode(0);
ListNode r = newHead ;
for(ListNode lnode:list)
{
r.next = lnode;
r = r.next;
}
r.next = null;
return newHead.next;
}
}
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if(pRoot==null)
{
return true;
}
return isReTree(pRoot.left,pRoot.right);
}
boolean isReTree(TreeNode left,TreeNode right)
{
if(left==null&&right==null)
{
return true;
}
if(left==null||right==null)
{
return false;
}
return (left.val==right.val)&&isReTree(left.left,right.right)
&&isReTree(right.left,left.right);
}
}
求一个二叉树的镜像
public class Solution {
public void Mirror(TreeNode root) {
if(root==null)
return;
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
Mirror(root.left);
Mirror(root.right);
}
}