leetcode Ch4-Binary Tree & BFS & Divide/Conquer

一、 

1. Lowest Common Ancestor

 1 class Solution {
 2 public:
 3     TreeNode *lowestCommonAncestor(TreeNode *root, TreeNode *A, TreeNode *B) {
 4         if (root == NULL || root == A || root == B) {
 5             return root;
 6         }
 7         TreeNode* left = lowestCommonAncestor(root->left, A, B);
 8         TreeNode* right = lowestCommonAncestor(root->right, A, B);
 9         if (left != NULL && right != NULL) {
10             return root;
11         }
12         if (left != NULL) {
13             return left;
14         }
15         if (right != NULL) {
16             return right;
17         }
18         return NULL;
19     }
20 };
View Code

refer : July,剑指offer

2. Lowest Common Ancestor of a Binary Search Tree

 1 class Solution {
 2 public:
 3     TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
 4         if (root == NULL || p == NULL || q == NULL) {
 5             return NULL;
 6         }
 7         if (root->val > p->val && root->val > q->val) {
 8             return lowestCommonAncestor(root->left, p, q);
 9         }
10         if (root->val < p->val && root->val < q->val) {
11             return lowestCommonAncestor(root->right, p, q);
12         }
13         return root;
14     }
15 };
View Code

二. Level order [BFS]

1. Binary Tree Level Order Traversal

 1 class Solution {
 2 public:
 3     vector<vector<int>> levelOrder(TreeNode* root) {
 4         vector<vector<int>> result;
 5         if (root == NULL) {
 6             return result;
 7         }
 8         queue<TreeNode*> q;
 9         q.push(root);
10         while(!q.empty()) {
11             int size = q.size();
12             vector<int> v;
13             for (int i = 0; i < size; i++) {
14                 TreeNode* tmp = q.front();
15                 q.pop();
16                 v.push_back(tmp->val);
17                 if (tmp->left != NULL) { 
18                     q.push(tmp->left);
19                 }
20                 if (tmp->right != NULL) {
21                     q.push(tmp->right);
22                 }
23             }
24             result.push_back(v);
25         }
26         return result;
27     }
28 };
View Code

2. Binary Tree Level Order Traversal II

 1 class Solution {
 2 public:
 3     vector<vector<int>> levelOrderBottom(TreeNode* root) {
 4         vector<vector<int>> result;
 5         if (root == NULL) {
 6             return result;
 7         }
 8         queue<TreeNode*> q;
 9         q.push(root);
10         while(!q.empty()) {
11             int size = q.size();
12             vector<int> v;
13             for (int i = 0; i < size; i++) {
14                 TreeNode* tmp = q.front();
15                 q.pop();
16                 v.push_back(tmp->val);
17                 if (tmp->left != NULL) { 
18                     q.push(tmp->left);
19                 }
20                 if (tmp->right != NULL) {
21                     q.push(tmp->right);
22                 }
23             }
24             result.push_back(v);
25         }
26         reverse(result.begin(), result.end());
27         return result;
28     }
29 };
View Code

在1的基础上多加一句reverse即可。

3. Binary Tree Zigzag Level Order Traversal

 1 class Solution {
 2 public:
 3     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
 4         vector<vector<int>> result;
 5         if (root == NULL) {
 6             return result;
 7         }
 8         queue<TreeNode*> q;
 9         q.push(root);
10         int count = 0;
11         while(!q.empty()) {
12             count++;
13             int size = q.size();
14             vector<int> v;
15             for (int i = 0; i < size; i++) {
16                 TreeNode* tmp = q.front();
17                 q.pop();
18                 v.push_back(tmp->val);
19                 if (tmp->left != NULL) { 
20                     q.push(tmp->left);
21                 }
22                 if (tmp->right != NULL) {
23                     q.push(tmp->right);
24                 }
25             }
26             if (count % 2 == 0) {
27                 reverse(v.begin(), v.end());    
28             }
29             result.push_back(v);
30         }
31         return result;
32     }
33 };
View Code

在1的基础上多加个count变量,偶数行就reverse一下即可

三、

1. Insert Node in a Binary Search Tree

 1 TreeNode* insertNode(TreeNode* root, TreeNode* node) {
 2     if (root == NULL) {
 3         return node;
 4     }
 5     if (node->val > root->val) {
 6         root->right = insertNode(root->right, node);
 7     } else {
 8         root->left = insertNode(root->left, node);
 9     }
10     return root;
11 }
View Code

2. Search Range in Binary Search Tree

code1: 

 1 class Solution {
 2 public:
 3     vector<int> searchRange(TreeNode* root, int k1, int k2) {
 4         helper(root, k1, k2);
 5         return result;
 6     }
 7     void helper(TreeNode* root, int k1, int k2) {
 8         if (root == NULL) {
 9             return;
10         }
11         if (k1 < root->val) {//说明左子树里有可能有
12             helper(root->left, k1, k2);
13         }
14         if (root->val >= k1 && root->val <= k2) {
15             result.push_back(root->val);
16         }
17         if (k2 > root->val) {
18             helper(root->right, k1, k2);
19         }
20     }
21 private:
22     vector<int>    result;
23 };
View Code

code2: 自己实现的,太繁琐。

 1 vector<int> searchRange(TreeNode* root, int k1, int k2) {
 2     vector<int> result;
 3     if (root == NULL) {
 4         return result;
 5     }
 6     if (root->val < k1) {
 7         return searchRange(root->right, k1, k2);
 8     }
 9     if (root->val > k2) {
10         return searchRange(root->left, k1, k2);
11     }
12     if (root->val >= k1 && root->val <= k2) {
13         vector<int> tmp1 = searchRange(root->left, k1, root->val - 1);
14         vector<int> tmp2 = searchRange(root->right, root->val + 1, k2);
15         result.insert(result.end(), tmp1.begin(), tmp1.end());
16         result.push_back(root->val);
17         result.insert(result.end(), tmp2.begin(), tmp2.end());
18     }
19     return result;
20 }
View Code

Binary Search Tree Iterator

 1 class BSTIterator {
 2 public:
 3     BSTIterator(TreeNode* root) {
 4         pushAll(root);
 5     }
 6     
 7     bool hasNext() {
 8         return (!myStack.empty());
 9     }
10 
11     int next() {
12         TreeNode* tmp = myStack.top();
13         myStack.pop();
14         pushAll(tmp->right);
15         return tmp->val;        
16     }
17     
18 private:
19     stack<TreeNode*> myStack;
20     void pushAll(TreeNode* node);
21 };
22 
23 void BSTIterator::pushAll(TreeNode* node) {
24     while (node != NULL) {
25         myStack.push(node);
26         node = node->left;
27     }
28 }
29 
30 /**
31  * Your BSTIterator will be called like this:
32  * BSTIterator i = BSTIterator(root);
33  * while (i.hasNext()) cout << i.next();
34  */
View Code

Remove Node in Binary Search Tree

Binary Tree Maximum Path Sum 

参见 ref 十五

Binary Tree Serialization

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

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

原文地址:https://www.cnblogs.com/forcheryl/p/4587829.html