原创 Q4.7 Find commen ancestor

Q: Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing ad

原创 Q4.6 find the ‘next’ node

Q: Write an algorithm to find the ‘next’ node (i.e., in-order successor) of a given node in a binary search tree where

原创 Q4.9 Find all paths which sum to a given value

Q:You are given a binary tree in which each node contains a value. Design an algorithm to print all paths which sum up

原创 Q4.8 To decide if T2 is a subtree of T1

Q:You have two very large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm

原创 Q2.7 Check if a list is a palindrome

Q:Check if a list is a palindrome A: 將鏈表的後半部分反轉,然後和前半部分分別比較 #include <iostream> using namespace std; struct ListNode

原创 Q3.7 Create a data structures to hold dogs and cats.(待續)

Q: An animal shelter holds only dogs and cats, and operates on a strictly "first in, first out" basis. People must adop

原创 Q2.4 Partition List

Q:Write code to partiton a linked list around a value x, such that all nodes less than x come before all nodes greater

原创 Q 4.4 creates a linked list of all the nodes at each depth

Q: Given a binary search tree, design an algorithm which creates a linked list of all the nodes at each depth (i.e., if

原创 Q.3.3 SetOfStacks

Q:  Imagine a (literal) stack of plates. If the stack gets too high, it m

原创 Q3.2 Stack with Min

Q; How would you design a stack which, in addition to push and pop, also has a function min which returns the minimum e

原创 Q2.5 Add Two Numbers

Q: You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in

原创 Q3.6 sort a stack in ascending order

Q:Write a program to sort a stack in ascending order. You should not make any assumptions about how the stack is implem

原创 Q.3.4 To solve the problem of hanoi

Q:  In the classic problem of the Towers of Hanoi, you have 3 rods and N

原创 Q2.6 Find the Beginning of the loop

Q:Given a circular linked list, implement an algorithm which returnsnode at the beginning of the loop. A: 思路1:可以用哈希表記錄

原创 Q3.1 Describe how you could use a single array to implement three stacks

Q:Describe how you could use a single array to implement three stacks A: 思路一:比較直觀的想法是將一個數組分爲三部分。將數組平分三部分, 每個部分維護一個棧頂指針。