LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder
1. Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted linked list: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
      0
     / \
   -3   9
   /   /
 -10  5
思路:对于树有关的问题还是首先想到用递归来解决,找到中间,那么左边是左子树,右边是右子树,以此递归即可。这个过程难点是找到中间的个链表节点,以及每次递归时确定左右子树的起始范围。
public class Solution {
    public TreeNode sortedListToBST(ListNode head) {
        if(head==null) return null;
        return toBST(head,null);
    }
    public TreeNode toBST(ListNode head, ListNode tail) { //每次递归时左右子树的起始,也就是链表中要取的头和尾
        ListNode slow = head;
        ListNode fast = head;
        if(head==tail) return null;
        while(fast!=tail&&fast.next!=tail) {  //这里很有意思,fast每次往后移动二步,slow每次往后移动一步,fast到达倒数第二个节点时,slow正好走到链表中间部分
            fast = fast.next.next;
            slow = slow.next;
        }
        TreeNode thead = new TreeNode(slow.val); //取当前的中间节点作为根节点
        thead.left = toBST(head,slow);  //对当前根节点的左边链表部分作左子树递归
        thead.right = toBST(slow.next,tail);  //对当前根节点的右边链表部分作右子树递归
        return thead;
    }
}
2. 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
思路:本来想通过层次便利来解题,因为隔了好久没有做算法题,突然还是有点手生。还是看了discuss一下,有一种比较简单的解法,还是从上往下一层层的来,对于当点节点cur,如果它有左子节点,那么按照题目的意思它必有右子节点,那么左子节点的next指向的是父节点的右子节点,对于这个右子节点来说,如果它的父节点的next不是null的话,那么这个右子节点的next指向的应该是它父节点的next节点的左子节点。这样从上往下循环即可。
public class Solution {
    public void connect(TreeLinkNode root) {
        TreeLinkNode level_start=root;
        while(level_start!=null){
            TreeLinkNode cur=level_start;
            while(cur!=null){
                if(cur.left!=null) cur.left.next=cur.right;
                if(cur.right!=null && cur.next!=null) cur.right.next=cur.next.left;
                cur=cur.next;
            }
            level_start=level_start.left;
        }
    }
}
上面的算法只对满二叉树的情形下有效,那么如果二叉树是任意的二叉树时又该怎么解题呢?比如说
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
解法和上题类似,还是一层层的从左往右遍历,对于当前层次上的节点cur,考虑其左右子节点的情况,这个过程需要一个prev来记录上一个链接中的上一个节点是什么,以及下一层的第一节点以便后续便利。
public class Solution {
    //based on level order traversal
    public void connect(TreeLinkNode root) {
        TreeLinkNode head = null; //head of the next level
        TreeLinkNode prev = null; //the leading node on the next level
        TreeLinkNode cur = root;  //current node of current level
        while (cur != null) {
            while (cur != null) { //iterate on the current level
                //left child
                if (cur.left != null) {
                    if (prev != null) {
                        prev.next = cur.left; // 如果左子节点不是空且前一个节点不是空,直接链接,如果前一个节点是空,由于cur是从左到右便利的,下层第一个不是空的子节点便是下层的第一个节点
                    } else {
                        head = cur.left;
                    }
                    prev = cur.left;  // 将链接好的节点置为prev节点
                }
                //right child
                if (cur.right != null) {
                    if (prev != null) {
                        prev.next = cur.right;
                    } else {
                        head = cur.right;
                    }
                    prev = cur.right;
                }
                //move to next node
                cur = cur.next;
            }
            //move to next level
            cur = head;
            head = null;
            prev = null;
        }
    }
}
3. Word Ladder
Given two words (beginWord and endWord), and a dictionary's word list, find the length of shortest transformation sequence from beginWord to endWord, such that:
- Only one letter can be changed at a time.
 - Each transformed word must exist in the word list. Note that beginWord is not a transformed word.
 
Note:
- Return 0 if there is no such transformation sequence.
 - All words have the same length.
 - All words contain only lowercase alphabetic characters.
 - You may assume no duplicates in the word list.
 - You may assume beginWord and endWord are non-empty and are not the same.
 
Example 1:
Input:
beginWord = "hit",
endWord = "cog",
wordList = ["hot","dot","dog","lot","log","cog"] Output: 5 Explanation: As one shortest transformation is "hit" -> "hot" -> "dot" -> "dog" -> "cog",
return its length 5.
Example 2:
Input:
beginWord = "hit"
endWord = "cog"
wordList = ["hot","dot","dog","lot","log"] Output: 0 Explanation: The endWord "cog" is not in wordList, therefore no possible transformation.
思路:可以使用BFS来解决这题,对于beginWord,和它临接的边是它在wordList中所能transform到的单词,这是第一层,那么第二层就是第一层所能transform到的所有单词,所以是个典型的BFS问题。试了下一般的BFS,如果从开始单词一层层的往外找,结果会超时,所以采取两端同时BFS,即从beginWord和endWord两端开始BFS。
public class Solution {
    public int ladderLength(String beginWord, String endWord, Set<String> wordList) {
        if(!wordList.contains(endWord)) return 0;
        Set<String> beginSet = new HashSet<String>(), endSet = new HashSet<String>();
        int len = 1;
        HashSet<String> visited = new HashSet<String>();
        beginSet.add(beginWord);
        endSet.add(endWord);
    //    如果从beginWord到endWord的BFS或者从endWord到beginWord的BFS过程中有哪一层是空的话,则表明beginWord到endWord走不通
        while (!beginSet.isEmpty() && !endSet.isEmpty()) {
            if (beginSet.size() > endSet.size()) {    // BFS从beginWord到endWord,交换BFS方向是为了减少计算次数
                Set<String> set = beginSet;
                beginSet = endSet;
                endSet = set;    // 这里交换beginSet和endSet来实现BFS方向的改变
            }
            Set<String> temp = new HashSet<String>();
            for (String word : beginSet) {
                char[] chs = word.toCharArray();
                for (int i = 0; i < chs.length; i++) {
                    for (char c = 'a'; c <= 'z'; c++) {
                        char old = chs[i];
                        chs[i] = c;
                        String target = String.valueOf(chs);
                        if (endSet.contains(target)) {     //     两端BFS过程中,如果相遇则直接返回BFS深度,还要加1才是题目要求的
                            return len + 1;
                        }
                        if (!visited.contains(target) && wordList.contains(target)) {
                            temp.add(target);
                            visited.add(target);
                        }
                        chs[i] = old;
                    }
                }
            }
            beginSet = temp;
            len++;
        }
        return 0;
    }
}
这里的 two-end BFS,以及BFS中改变遍历的方向以减少计算量还是很有意思的。
LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder的更多相关文章
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
		
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
 - 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree
		
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
 - 【LeetCode OJ】Convert Sorted Array to Binary Search Tree
		
Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...
 - Leetcode No.108 Convert Sorted Array to Binary Search Tree(c++实现)
		
1. 题目 1.1 英文题目 Given an integer array nums where the elements are sorted in ascending order, convert ...
 - 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
 - 【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree
		
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
 - 【LeetCode OJ】Convert Sorted List to Binary Search Tree
		
Problem Link: http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ We design a ...
 - LeetCode OJ 108. Convert Sorted Array to Binary Search Tree
		
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 把一 ...
 - 【LeetCode】108. Convert Sorted Array to Binary Search Tree
		
Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...
 
随机推荐
- bzoj3694: 最短路(树链剖分/并查集)
			
bzoj1576的帮我们跑好最短路版本23333(双倍经验!嘿嘿嘿 这题可以用树链剖分或并查集写.树链剖分非常显然,并查集的写法比较妙,涨了个姿势,原来并查集的路径压缩还能这么用... 首先对于不在最 ...
 - 【初级算法】2.买卖股票的最佳时机 II
			
题目: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参与多笔交易(你必 ...
 - 使用Google的CDN JQuery库
			
CDN的全称是Content Delivery Network,即内容分发网络.其目的是通过在现有的Internet中增加一层新的网络架构,将网站的内容发布到最接近用户的网络"边缘" ...
 - 理清一下JavaScript面向对象思路
			
借这篇文章理清一下自己的思路,同时也希望能给和我同样一知半解的同学理清一下思路.引发思考来自于我犯的一个错误,错误代码是这样的: 1 var o = { 2 ... 3 } 4 var obj ...
 - bzoj [POI2005]Kos-Dicing 二分+网络流
			
[POI2005]Kos-Dicing Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1835 Solved: 661[Submit][Status][ ...
 - webpack插件url-loader使用规范
			
其实说到性能优化,他的范围太广了,今天我们就只聊一聊通过webpack配置减少http请求数量这个点吧. 简单说下工作中遇到的问题吧,我们做的一个项目中首页用了十多张图片,每张图片都是一个静态资源,所 ...
 - java发送邮件(一)
			
一:前言 一直想做有关java发邮件的功能,但是了一直没有成功,特别的无语啊,所以今天有时间我终于成功了啊,虽然是最简单的,但是还是记载下来吧! 二:内容 这里主要需要的是spring-context ...
 - PHP日期时间操作
			
一.设置时区 date_default_timezone_set('PRC'); 二.获取当前时间的 Unix 时间戳(格林威治时间 1970 年 1 月 1 日 00:00:00到当前时间的秒数)和 ...
 - css 实现元素水平垂直居中总结5中方法
			
个人总结,如有错误请指出,有好的建议请留言.o(^▽^)o 一.margin:0 auto:text-align:center:line-height方法 <div id="divAu ...
 - 原生js addclass,hasClass,removeClass,toggleClass的兼容
			
(function (window) { 'use strict'; // class helper functions from bonzo https://github.com/ded/bonzo ...