一、

1. Lowest Common Ancestor

 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

Binary Tree Maximum Path Sum

参见 ref十五

Binary Tree Serialization

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

对于n个数的数组,一个数x如果从左往右数是第k个数,那么从右往左数的话是第(n - k + 1)个数。

leetcode Ch4-Binary Tree & BFS & Divide/Conquer的更多相关文章

  1. [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 ...

  2. leetcode 199 :Binary Tree Right Side View

    // 我的代码 package Leetcode; /** * 199. Binary Tree Right Side View * address: https://leetcode.com/pro ...

  3. 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 ...

  4. (二叉树 递归) leetcode 145. Binary Tree Postorder Traversal

    Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,null,2, ...

  5. 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 这个题实际上就是把每一行最右侧的树打印出来,所以实际上还是一个层次遍历. 依旧利用之前层次遍历的代码,每次大的循环存 ...

  6. [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 ...

  7. LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

    翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...

  8. LeetCode—— Invert Binary Tree

    LeetCode-- Invert Binary Tree Question invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 ...

  9. 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 ...

随机推荐

  1. (转)CentOS7安装KVM虚拟机详解

    原文:https://github.com/jaywcjlove/handbook/blob/master/CentOS/CentOS7%E5%AE%89%E8%A3%85KVM%E8%99%9A%E ...

  2. Linux 进程以及多线程的支持

    1.最初内核并没有实现对多线程的支持,2.6之后开始以轻量级进程的方式对多线程进行支持(轻量级线程组). a.在2.6 之前,如果需要实现多线程,只能在用户态下实现,用户程序自己控制线程的切换, 实际 ...

  3. Session 与 Cookie

    Session 与 Cookie 的作用都是为了保持访问用户与后端服务器的交互状态.它们本身只是HTTP中的一个配置项,在servlet规范中也只对应一个类而已. 理解 Cookie 通俗地说就是当一 ...

  4. WPF简单MVVM实现

    1. MVVM介绍: MVVM就是: Model -- 模型(现实中对象的抽象) View -- UI(用户界面) ViewModel -- UI界面的抽象(给View提供数据,并响应View的操作) ...

  5. Java reflect 反射 1

    1 反射的概述 反射含义:可以获取正在运行的Java对象. JAVA反射机制是在运行状态中,对于任意一个类,都能够得到这个类的所有属性和方法; 对于任意一个对象,都能够调用它的任意一个方法; 这种动态 ...

  6. 【转】MVC Model建模及Entity Framework Power Tool使用

    MVC如使用Code-First代码优先约定,先建实体类,再根据实体类创建数据库. 在创建实体类后,新建一个数据上下文类,如下: publicclassMusicStoreDB : DbContext ...

  7. Oracle中的自连接(self join)-当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自连接。

    http://blog.163.com/wkyuyang_001/blog/static/10802122820091751049479/ 当表中的某一个字段与这个表中另外字段的相关时,我们可能用到自 ...

  8. JAVA练手--数组

    //数组 public static void main(String[] args) { //1. 数组排序和查找 { int[] intA = {5, 4, 2, 3, 1}; String[] ...

  9. Android studio应用导入源码错误This attribute must be localized

    This attribute must be localized 产生原因: 多语言错误,源码中关于语言的显示不能直接赋值,而是需要通过xml来实现: 例如 <TextView android: ...

  10. PL/SQL之异常

    异常分为预定义异常和用户自定义异常.预定义异常是由系统定义的异常.由于他们已在STANDARD包中预定义了,因此,这些预定义异常可以直接在程序中使用,而不用在预定义部分声明.而用户自定义异常则需要在定 ...