leetcode Ch4-Binary Tree & BFS & Divide/Conquer
一、
class Solution {
public:
TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) {
if (root == NULL || root == A || root == B) {
return root;
}
TreeNode* left = lowestCommonAncestor(root->left, A, B);
TreeNode* right = lowestCommonAncestor(root->right, A, B);
if (left != NULL && right != NULL) {
return root;
}
if (left != NULL) {
return left;
}
if (right != NULL) {
return right;
}
return NULL;
}
};
refer : July,剑指offer
2. Lowest Common Ancestor of a Binary Search Tree
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if (root == NULL || p == NULL || q == NULL) {
return NULL;
}
if (root->val > p->val && root->val > q->val) {
return lowestCommonAncestor(root->left, p, q);
}
if (root->val < p->val && root->val < q->val) {
return lowestCommonAncestor(root->right, p, q);
}
return root;
}
};
二. Level order [BFS]
1. Binary Tree Level Order Traversal
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
vector<vector<int>> result;
if (root == NULL) {
return result;
}
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
int size = q.size();
vector<int> v;
for (int i = ; i < size; i++) {
TreeNode* tmp = q.front();
q.pop();
v.push_back(tmp->val);
if (tmp->left != NULL) {
q.push(tmp->left);
}
if (tmp->right != NULL) {
q.push(tmp->right);
}
}
result.push_back(v);
}
return result;
}
};
2. Binary Tree Level Order Traversal II
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> result;
if (root == NULL) {
return result;
}
queue<TreeNode*> q;
q.push(root);
while(!q.empty()) {
int size = q.size();
vector<int> v;
for (int i = ; i < size; i++) {
TreeNode* tmp = q.front();
q.pop();
v.push_back(tmp->val);
if (tmp->left != NULL) {
q.push(tmp->left);
}
if (tmp->right != NULL) {
q.push(tmp->right);
}
}
result.push_back(v);
}
reverse(result.begin(), result.end());
return result;
}
};
在1的基础上多加一句reverse即可。
3. Binary Tree Zigzag Level Order Traversal
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> result;
if (root == NULL) {
return result;
}
queue<TreeNode*> q;
q.push(root);
int count = ;
while(!q.empty()) {
count++;
int size = q.size();
vector<int> v;
for (int i = ; i < size; i++) {
TreeNode* tmp = q.front();
q.pop();
v.push_back(tmp->val);
if (tmp->left != NULL) {
q.push(tmp->left);
}
if (tmp->right != NULL) {
q.push(tmp->right);
}
}
if (count % == ) {
reverse(v.begin(), v.end());
}
result.push_back(v);
}
return result;
}
};
在1的基础上多加个count变量,偶数行就reverse一下即可
三、
1. Insert Node in a Binary Search Tree
TreeNode* insertNode(TreeNode* root, TreeNode* node) {
if (root == NULL) {
return node;
}
if (node->val > root->val) {
root->right = insertNode(root->right, node);
} else {
root->left = insertNode(root->left, node);
}
return root;
}
2. Search Range in Binary Search Tree
code1:
class Solution {
public:
vector<int> searchRange(TreeNode* root, int k1, int k2) {
helper(root, k1, k2);
return result;
}
void helper(TreeNode* root, int k1, int k2) {
if (root == NULL) {
return;
}
if (k1 < root->val) {//说明左子树里有可能有
helper(root->left, k1, k2);
}
if (root->val >= k1 && root->val <= k2) {
result.push_back(root->val);
}
if (k2 > root->val) {
helper(root->right, k1, k2);
}
}
private:
vector<int> result;
};
code2: 自己实现的,太繁琐。
vector<int> searchRange(TreeNode* root, int k1, int k2) {
vector<int> result;
if (root == NULL) {
return result;
}
if (root->val < k1) {
return searchRange(root->right, k1, k2);
}
if (root->val > k2) {
return searchRange(root->left, k1, k2);
}
if (root->val >= k1 && root->val <= k2) {
vector<int> tmp1 = searchRange(root->left, k1, root->val - );
vector<int> tmp2 = searchRange(root->right, root->val + , k2);
result.insert(result.end(), tmp1.begin(), tmp1.end());
result.push_back(root->val);
result.insert(result.end(), tmp2.begin(), tmp2.end());
}
return result;
}
Binary Search Tree Iterator
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();
*/
Remove Node in Binary Search Tree
参见 ref十五
===================================================
对于n个数的数组,一个数x如果从左往右数是第k个数,那么从右往左数的话是第(n - k + 1)个数。
leetcode Ch4-Binary Tree & BFS & Divide/Conquer的更多相关文章
- [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- leetcode 199 :Binary Tree Right Side View
// 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...
- LeetCode:Construct Binary Tree from Inorder and Postorder Traversal,Construct Binary Tree from Preorder and Inorder Traversal
LeetCode:Construct Binary Tree from Inorder and Postorder Traversal Given inorder and postorder trav ...
- (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal
Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...
- leetcode 199. Binary Tree Right Side View 、leetcode 116. Populating Next Right Pointers in Each Node 、117. Populating Next Right Pointers in Each Node II
leetcode 199. Binary Tree Right Side View 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...
- [LeetCode] 549. Binary Tree Longest Consecutive Sequence II_ Medium tag: DFS recursive
Given a binary tree, you need to find the length of Longest Consecutive Path in Binary Tree. Especia ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- LeetCode—— Invert Binary Tree
LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...
- Java for LeetCode 107 Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left ...
随机推荐
- (转)总结之:CentOS 6.5 MySQL数据库的基础以及深入详解
总结之:CentOS 6.5 MySQL数据库的基础以及深入详解 原文:http://tanxw.blog.51cto.com/4309543/1395539 前言 早期MySQL AB公司在2009 ...
- deepin安装php5.6
sudo su -echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu xenial main" | tee -a /etc/a ...
- hibernate关联关系的crud之级联
cascade级联,只会影响CRUD的CUD,不会影响读取.不设置级联,从多的一方能读出一的一方,设了级联,从一的一方,默认也不能读出多的一方. 如果两个对象之间有关联,不管是一对多,多对一,单向还是 ...
- 我爱Markdown (3)
继续Markdown的常见语法, 本文将介绍: 07 - Links 链接 08 - Images 图片 09 - Blockquotes 块引用 10 - Backslash Escapes 显示保 ...
- 12 Callable & Future & FutureTask
创建线程的2种方式,一种是直接继承Thread,另外一种就是实现Runnable接口. 这2种方式都有一个缺陷就是:在执行完任务之后无法获取执行结果. 如果需要获取执行结果,就必须通过共享变量或者使用 ...
- golang的bytes.NewReader函数出现的问题
在我试图装入一个300mb的数据时,发生了溢出. 我本以为不会出现这种问题的(内存和硬盘都够用),可见golang的bytes包还是设置了容量限制的. 虽然通常来说300mb的[]byte不管什么情况 ...
- 《高质量c++/c编程指南》学习摘要
1. 尽可能在定义变量的同时初始化该变量(就近原则)——防止忘记初始化,引用未被初始化的变量,可能导致程序错误 2. 代码行最大长度宜控制在70~80个字符以内(长行拆分)——否则眼睛看不过来,也不便 ...
- [PY3]——时间处理——datetime | calendar
Python3的日期/时间处理模块 datetime的格式化符号 格式化符号 表示 %y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 月份(01-12) %d 日 ...
- 9.Symbol
Symbol Symbol 概述 ES5 的对象属性名都是字符串,这容易造成属性名的冲突.比如,你使用了一个他人提供的对象,但又想为这个对象添加新的方法(mixin 模式),新方法的名字就有可能与现有 ...
- [转]flash.net.Socket
本文转自:http://designstacks.net/actionscript-3-new-capabilities http://help.adobe.com/en_US/ActionScrip ...