e***n 发帖数: 42 | 1 BST 找in order traversal 的下一个元素只需把 in order traversal 的代码稍加改
动:
以C程序为例:
void traverse(node* currNode, int targetValue, bool targetOn) {
if(currNode == NULL)
return;
/* When target if find, the current node is the node next to target node
in the in-order traversal sequence */
if (targetOn == TRUE) {
printf("%d", currNode->value);
exit(); // mission complete, terminate the program;
}
traverse(currNode->lChild, targetValue, targetOn);
/* Indic... 阅读全帖 |
|
l*****a 发帖数: 14598 | 2 刚才面了个contractor
面了道简单的,print binary tree level by level
为节省时间,直接让他去collabedit,我干我的工作
class node{
private int data;
private Node left;
private Node right;
private node parent;
private int level;
public LinkedList listofNode;
public int getData()
{
return this.data;
}
public node getLeftNode()
{
return this.left;
}
public node getRightNode()
{
return this.right;
}
public printTree(Node node )
{
... 阅读全帖 |
|
k*******2 发帖数: 84 | 3 Not sure whether it is right;
I think the space cost is O(log(n));
The time cost seems to be hard to analyze; But I think it ll not be O(n^3);
#define tr(c, i) for(typeof((c).begin()) i = (c).begin(); i != (c).end(); i+
+ )
unordered_map > findPath(TreeNode *root, int sum)
{
unordered_map > sumAtRoot;
if(root == NULL)
return sumAtRoot;
unordered_map > sumAtLeft = findPath(root->left, sum);
unordered_map阅读全帖 |
|