您的位置:首页 > 产品设计 > UI/UE

leetcode Ch4-Binary Tree & BFS & Divide/Conquer

2015-06-19 10:16 597 查看
一、

1. Lowest Common Ancestor

class BSTIterator {
public:
BSTIterator(TreeNode* root) {
pushAll(root);
}

bool hasNext() {
return (!myStack.empty());
}

int next() {
TreeNode* tmp = myStack.top();
myStack.pop();
pushAll(tmp->right);
return tmp->val;
}

private:
stack<TreeNode*> myStack;
void pushAll(TreeNode* node);
};

void BSTIterator::pushAll(TreeNode* node) {
while (node != NULL) {
myStack.push(node);
node = node->left;
}
}

/**
* Your BSTIterator will be called like this:
* BSTIterator i = BSTIterator(root);
* while (i.hasNext()) cout << i.next();
*/


View Code

Remove Node in Binary Search Tree

Binary Tree Maximum Path Sum

参见 ref 十五

Binary Tree Serialization

===================================================

对于n个数的数组,一个数x如果从左往右数是第k个数,那么从右往左数的话是第(n - k + 1)个数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: