leetCode解题报告5道题(八)

题目一:

Populating Next Right Pointers in Each Node

Given a binary tree

    struct TreeLinkNode {
      TreeLinkNode *left;
      TreeLinkNode *right;
      TreeLinkNode *next;
    }

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.

Initially, all next pointers are set to NULL.

Note:

  • You may only use constant extra space.
  • You may assume that it is a perfect binary tree (ie, all leaves are at the same level, and every parent has two children).

For example,

Given the following perfect binary tree,

         1
       /        2    3
     / \  /     4  5  6  7

After calling your function, the tree should look like:

         1 -> NULL
       /        2 -> 3 -> NULL
     / \  /     4->5->6->7 -> NULL

分析:

题意给我们一棵“完全二叉树”,让我们把二叉树中每个结点的next域根据如例子那样给它赋值。

看到这个题目,我们的第一反应可能是

1、层次遍历二叉树(这样要用到一个queue队列,并不满足题意的对空间的要求)

2、递归方法(这个是可以的)

但因为这道题目很特殊,二叉树是一棵完全二叉树,所以我们现在要用循环迭代的方法来求解这道题。

具体看代码中的注释

AC代码:

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if (root == null)
            return ;
        //每一层的最左边的那个结点
        TreeLinkNode leftNode = root;
        while (leftNode != null){
            //把同一层的结点用next串起来
            TreeLinkNode nowNode = leftNode;
            while (nowNode != null){
                //左孩子的next域指向右孩子
                if (nowNode.left != null){
                    nowNode.left.next = nowNode.right;
                }
                //如果一个结点nowNode的右孩子不为空,却这个结点nowNode还有兄弟的话
                //那么结点nowNode的右孩子的next域要指向兄弟结点的left域
                if (nowNode.right != null && nowNode.next != null){
                    nowNode.right.next = nowNode.next.left;
                }
                //处理结点nowNode的右兄弟(如果为null则不处理)
                nowNode = nowNode.next;
            }
            leftNode = leftNode.left;
        }
    }

}

题目二:

Populating Next Right Pointers in Each Node II

Follow up for problem "Populating Next Right Pointers in Each Node".

What if the given tree could be any binary tree? Would your previous solution still work?

Note:

  • You may only use constant extra space.

For example,

Given the following binary tree,

         1
       /        2    3
     / \        4   5    7

After calling your function, the tree should look like:

         1 -> NULL
       /        2 -> 3 -> NULL
     / \        4-> 5 -> 7 -> NULL

分析:

这题跟上面那个题目的区别在于条件“所给二叉树并不一定是完全二叉树”了,这样子,在上一题的基础上,我们需要去找到每一层的第一个结点,找到这个第一个满足的结点,其他的思路跟上一题是一样的。

AC代码:

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */
public class Solution {
    public void connect(TreeLinkNode root) {
        if (root == null)
            return ;
        //每一层的第一个结点
        TreeLinkNode firstNode = root;

        while (firstNode != null){
            TreeLinkNode nowNode = firstNode;

            //找到第一个不为null的结点.
            TreeLinkNode tempNode = null;
            while (nowNode != null){
                if (nowNode.left != null){
                   tempNode = nowNode.left;
                   break;
                }
                if (nowNode.right != null){
                   tempNode = nowNode.right;
                   break;
                }
                nowNode = nowNode.next;
            }

            //下一层要开始的第一个parent结点
            firstNode = tempNode;

            //把这一层的所有结点用next域串起来
            while (nowNode != null){
                if (nowNode.left != null && nowNode.left != tempNode){
                    tempNode.next = nowNode.left;
                    tempNode = nowNode.left;
                }
                if (nowNode.right != null && nowNode.right != tempNode){
                    tempNode.next = nowNode.right;
                    tempNode = nowNode.right;
                }
                nowNode = nowNode.next;
            }
        }
    }
}

题目三:

Pascal‘s Triangle

Given numRows, generate the first numRows of Pascal‘s triangle.

For example, given numRows = 5,

Return

[
     [1],
    [1,1],
   [1,2,1],
  [1,3,3,1],
 [1,4,6,4,1]
]

AC代码:

public class Solution {
    public ArrayList<ArrayList<Integer>> generate(int numRows) {
        ArrayList<ArrayList<Integer>> arrays = new ArrayList<ArrayList<Integer>>();
        for(int i=0; i<numRows; ++i){
            ArrayList<Integer> list = new ArrayList<Integer>();
            for (int j=0; j<=i; ++j){
                if (j == 0 || j == i){
                    list.add(1);
                }else{
                    ArrayList<Integer> temp = arrays.get(i-1);
                    int left = temp.get(j-1);
                    int right = temp.get(j);
                    list.add(left+right);
                }
            }
            arrays.add(list);
        }
        return arrays;
    }
}

题目四:Pascal‘s Triangle II

Given an index k, return the kth row of the Pascal‘s triangle.

For example, given k = 3,

Return [1,3,3,1].

Note:

Could you optimize your algorithm to use only O(k) extra space?

分析:

这道题目和上面那道题目的思路基本上是一样的arrays[i] = arrays[i] + arrays[i-1].

但是由于题目要求给我们的空间就O(k),也就是说当我们想改变下一行的值arrays[i]的话我们必须从中间向左边改变,然后右边用r-i 来对称设置值,程序一开始我们先设置出k个位置的值,每个值都为1。

比如 我们要求的K=4 则

r <= k;

r=0:   [1,1,1,1,1]

r=1: [1,1,1,1,1];

r=2:[1,2,1,1,1];

r=3:[1,3,3,1,1];

k=4 :[1,4,6,4,1]

大家看看我的代码,如果我们不用
“从中间向左边改变,然后右边用r-i 来对称设置值”这样的话,会出现什么问题!

AC代码:

public class Solution {
    public ArrayList<Integer> getRow(int rowIndex) {
        //csdn : http://blog.csdn.net/ljphhj
        ArrayList<Integer> list = new ArrayList<Integer>();
        for (int i=0; i<=rowIndex; ++i){
            list.add(1);
        }
        for(int i=0; i<=rowIndex; ++i){
            //从中间i/2往左移动设置
            //避免你从左向中间时,你计算的当前值会影响到下一个位的计算
            for (int j=i/2; j>=0; --j){
                if (j != 0 && j != i){
                    int left = list.get(j-1);
                    int right = list.get(j);
                    list.set(j, left+right);//设置值
                    list.set(i-j, left+right);//对称另一边的值
                }
            }
        }
        return list;
    }
}

题目五:

Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.

For example, given the following triangle

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 =
11).

Note:

Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.

分析:

这题为典型的DP问题,求最短路径长度,也可以用递归的方法来求.

设用一个数组arr[i][j] 来存储这个三角形。

主要思想:动态规划通常用来求最优解,能用动态规划解决的求最优解问题,必须满足,最优解的每个局部解也都是最优的。以上题为例,最佳路径上面的每个数字到底部的那一段路径,都是从该数字出发到达到底部的最佳路径。

则从三角形的最后一行向上求出每个点对应的最短路径,并把值存下来,最终arr[0][0]就为我们要得到的值

此题目中

最后一行的arr[3][]  = {4,1,8,3};

则当到倒数第二行时,每个结点arr[2][i] 的最短路径为 arr[2][i] =  min{arr[2][i] + arr[2+1][i]  ,  arr[2][i] + arr[2+1][i+1] };

这样我们就可以得到倒数第二行的所有点到叶子的最短路径长度了,一直往上,直到第一行就得到了最后的结果arr[0][0]

AC代码:

public class Solution {
    public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
        int len1 = triangle.size();
        //从倒数第二行起始
        for (int i=len1-2; i>=0; i--){
            ArrayList<Integer> listUp = triangle.get(i);//当前行
            ArrayList<Integer> listDown = triangle.get(i+1);//当前行的下一行
            for (int j=0; j<listUp.size(); ++j){
                int left = listDown.get(j);
                int right = listDown.get(j+1);
                int min = left > right ? right+listUp.get(j) : left+listUp.get(j);
                listUp.set(j, min);
            }
        }
        return triangle.get(0).get(0);
    }
}

leetCode解题报告5道题(八),布布扣,bubuko.com

时间: 2024-10-19 19:21:33

leetCode解题报告5道题(八)的相关文章

leetCode解题报告5道题(十)

Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2368    Accepted Submission(s): 333 Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景.磁

leetCode解题报告5道题(九)

题目一:Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For example,If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ] 分析: 题意给我们一个数字n, 和一个数字k,让我们求出从 1~~n中取出k个数所能得到的组合数 所

leetCode解题报告5道题(十一)

题目一:Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2]

leetCode解题报告5道题(六)

题目一: Longest Substring Without Repeating Characters Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the

leetCode解题报告5道题(七)

先送上亚马逊传送门:<黑客与画家>:硅谷创业之父 Paul Graham 文集 再送上一个思维导图: 最好的办法就是自己创业或者加入创业公司 一个命题 命题 创业是一个压缩的过程,所有工作压缩成短短几年. 你不再是低强度的工作四十年,而是以极限强度工作四年 举例解释 一个优秀的黑客去除各种障碍,工作效率可以是在公司时的36倍. 假设他年薪8万美元,那么一个勤奋工作,摆脱杂事干扰的聪明黑客, 他的工作相当于年薪200万美元的价值 这里说的是极限情况,休闲时间为0,工作强度足以危害到健康. 守恒定

LeetCode解题报告:LRU Cache

LRU Cache Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and set. get(key) - Get the value (will always be positive) of the key if the key exists in the cache, otherwise retu

LeetCode解题报告:Reorder List

Reorder List Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must do this in-place without altering the nodes' values. For example,Given {1,2,3,4}, reorder it to {1,4,2,3}. 思路: 1.利用快慢两个指针将链表一分为二: 2.针对第二个子链表求倒序

LeetCode解题报告:Linked List Cycle &amp;&amp; Linked List Cycle II

Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using extra space? Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follo

LeetCode解题报告:Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti