116Populating 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

 

注意這題的需求


1. use only extra constant space       ----> cann't use resursive. use iterative to do the recursive work


2.perfect binary tree --> each nonleaf node has two child


這題基本上就是一個node做好兩件事情,


1. 把自己的 left child next 跟 right child 接好


2.如果自己有next,就把right child next 也跟 自己的next.left接好


然後一層一層做就可以了


angledark0123 發表在 痞客邦 留言(0) 人氣()

341Flatten Nested List Iterator

Given a nested list of integers, implement an iterator to flatten it.


Each element is either an integer, or a list -- whose elements may also be integers or other lists.


Example 1:

Given the list [[1,1],2,[1,1]],


By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].


 


Example 2:

Given the list [1,[4,[6]]],


By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].


 

這一題要注意的地方是 NestedInteger 要在 hasNext() 裡面展開,而不是在next()


因為如果展開後沒有integer 要return false


ex.[ [ ] ] 的測資


如果是在next 裡面展開就會錯誤,因為實際上根本沒有可以return的 integer


angledark0123 發表在 痞客邦 留言(0) 人氣()


 










444. Wildcard Matching



angledark0123 發表在 痞客邦 留言(0) 人氣()

621Task Scheduler
Given a char array representing tasks CPU need to do. It contains capital letters A to Z where different letters represent different tasks.Tasks could be done without original order. Each task could be done in one interval. For each interval, CPU could finish one task or just be idle.

angledark0123 發表在 痞客邦 留言(0) 人氣()

56Merge Intervals
Given a collection of intervals, merge all overlapping intervals.

angledark0123 發表在 痞客邦 留言(0) 人氣()

761. Special Binary String
Special binary strings are binary strings with the following two properties:
 

angledark0123 發表在 痞客邦 留言(0) 人氣()

139Word Break
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.

angledark0123 發表在 痞客邦 留言(0) 人氣()

236Lowest Common Ancestor of a Binary Tree 

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.
According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”
_______3______
/ \
___5__ ___1__
/ \ / \
6 _2 0 8
/ \
7 4

For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
這題其實當初我想得太複雜了.....(注意!其實很多題目都是這樣,看清楚題目給的條件,有時候題目加上限制後會變得非常簡單)
如果今天是要找不知道存不存在的兩個node 那這個解法一定不行,但是這題因為是確定存在樹上的兩點node,所以就變得非常簡單
只要思考這兩個node會在樹上存在的可能組合就能找出這組的答案了
一種就是分別在左右subtree,一種就是都在同一個subtree,
左右的情形就是都存在時,那個node就是root了,而當在同一個subtree時,就是第一個相等的那個node開始return就好

angledark0123 發表在 痞客邦 留言(0) 人氣()

 69Sqrt(x)

Implement int sqrt(int x).
Compute and return the square root of x.
x is guaranteed to be a non-negative integer.


Example 1:
Input: 4
Output: 2

 
Example 2:
Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated.

 
 這題我只想到暴力解,所以就直接放棄看解答了,解法非常有趣
用BFS,不斷逼近正確的區段,因為有可能不是整數,最後確定數值是用 if(mid+1 > x/(mid+1)) 來判斷  (來確定下個值會過大,正解就是這個值)

angledark0123 發表在 痞客邦 留言(0) 人氣()


17Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below.

angledark0123 發表在 痞客邦 留言(0) 人氣()

88Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.

angledark0123 發表在 痞客邦 留言(0) 人氣()

 234Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.
Follow up:
Could you do it in O(n) time and O(1) space?

angledark0123 發表在 痞客邦 留言(0) 人氣()

Blog Stats
⚠️

成人內容提醒

本部落格內容僅限年滿十八歲者瀏覽。
若您未滿十八歲,請立即離開。

已滿十八歲者,亦請勿將內容提供給未成年人士。