Use the definition of ListNode below for a doubly linked list of strings. Assume that lists do NOT have header nodes.
TreeNode with a parent pointer.
+---+ +---+ +---+ +---+
list---> | |-->| |-->| |-->| |->|||
| A | | B | | C | | D |
|||<--| |<--| |<--| |<--| }
+---+ +---+ +---+ +---+
Then after the call list = moveToFront(list,"C")
the picture should be:
+---+ +---+ +---+ +---+
list---> | |-->| |-->| |-->| |->|||
| C | | A | | B | | D |
|||<--| |<--| |<--| |<--| }
+---+ +---+ +---+ +---+
Be sure to unlink/relink all next/prev pointers, but be careful about
special cases like moving the first or last nodes of the list.
Thanks to Nick Parlante for this problem.
In a binary search tree, each node contains a single data element and
"small" and "large" pointers to sub-trees (i.e. left and
right). A binary tree may be defined as follows:
Below is a binary search tree with the
numbers 1 through 5.
Now, consider the data as a circular doubly linked list.
The challenge is to take an ordered binary tree and rearrange the internal pointers to make a circular doubly linked list out of it. The left pointer should play the role of prev and the right pointer should play the role of next. The list should be arranged so that the nodes are in increasing order.
treeToList(TLNode root) that takes an ordered
binary tree and rearranges the internal pointers to make a circular
doubly linked list out of the tree nodes. The prev pointers
should be stored in the left field and the next pointers should be
stored in the right field. The list should be arranged so that the
nodes are in increasing order. Return the head pointer to the new
list. The operation can be done in O(n) time -- essentially operating
on each node once.
For example, in the tree shown below there are exactly four root-to-leaf paths. The sums of the paths are 27, 22, 26, and 18. Thus the value of hasPathSum(t,27) should be true and the value of hasPathSum(t,30) should be false (assuming t points to the root of the tree --- the node whose info field has value 5.)
Hint: You should make two recursive calls, you must decide on the value of target for each recursive call so that you can use the return values of the recursive calls to solve the problem. Note: in a tree with one node (a leaf) you can determine immediately if there's a path that sums to the target based on whether the target and the node are equal.
For example, in the tree t shown below (t points to the root), findKthInOrder(t,4) returns a reference/pointer to the node with value 9, findKthInOrder(t,8) returns a reference/pointer to the node with value 18, and findKthInOrder(t,12) returns null since there are only 9 nodes in the tree.
The method count discussed in lecture returns the number of nodes in a tree.
findKthInOrder and give the
solution to the recurrence.
findKthInOrder
for the worst case, i.e., when a tree is completely unbalanced
and the method is called to do the most work on every recursive call.
What is the solution to this recurrence?