1--The nearest common ancestor of the binary tree
Main idea:
There are only two cases of the nearest ancestor: ① bottom-up, when the two destination nodes are in the left and right subtrees of the current node, the current node is the nearest ancestor of the two destination nodes; ② the nearest ancestor and one of the destination nodes points are the same, another destination node is on the subtree of the destination node;
Recursively search for the destination node, when the destination node is found, return to the destination node, otherwise return NULL; when a node finds two destination nodes on the left and right subtrees, it indicates that this node is the nearest ancestor; otherwise Return the return node of the subtree that is not empty (then the two nodes correspond to the second case);
#include <iostream>
#include <vector>
#include <stack>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
if(root == NULL || root->val == p->val || root->val == q->val) return root;
TreeNode* left = lowestCommonAncestor(root->left, p, q);
TreeNode* right = lowestCommonAncestor(root->right, p, q);
if(left != NULL && right != NULL) return root;
else if( left != NULL) return left;
else return right;
}
};
int main(int argc, char *argv[]){
TreeNode *Node1 = new TreeNode(3);
TreeNode *Node2 = new TreeNode(5);
TreeNode *Node3 = new TreeNode(1);
TreeNode *Node4 = new TreeNode(6);
TreeNode *Node5 = new TreeNode(2);
TreeNode *Node6 = new TreeNode(0);
TreeNode *Node7 = new TreeNode(8);
TreeNode *Node8 = new TreeNode(7);
TreeNode *Node9 = new TreeNode(4);
Node1->left = Node2;
Node1->right = Node3;
Node2->left = Node4;
Node2->right = Node5;
Node3->left = Node6;
Node3->right = Node7;
Node5->left = Node8;
Node6->right = Node9;
Solution S1;
TreeNode* res = S1.lowestCommonAncestor(Node1, Node2, Node9);
std::cout << res->val << std::endl;
return 0;
}
2--Inorder successor node of binary search tree
Main idea:
If the p node has a right subtree, return the leftmost node of its right subtree (definition of inorder traversal);
If the p node has no right subtree, start looking for the parent node of p node from the root node; (according to the definition of binary search tree, you can save the time of searching, just search on one side);
#include <iostream>
#include <vector>
#include <stack>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
TreeNode *res = NULL;
if(p->right != NULL){ // p的中序后继是其右子树上最左的结点(即右字数上最先返回的结点)
res = p->right;
while(res->left != NULL) res = res->left;
return res;
}
// p没有右子树,从root结点开始搜索p的父亲结点
while(root != NULL){
if(root->val > p->val){ // p在左子树上
res = root;
root = root->left; // 在左子树上找到最后一个比p大的结点(中序遍历是有序的,中序后继结点表明是比p结点大)
}
else{
root = root->right; // p在右子树上
}
}
return res;
}
};
int main(int argc, char *argv[]){
TreeNode *Node1 = new TreeNode(2);
TreeNode *Node2 = new TreeNode(1);
TreeNode *Node3 = new TreeNode(3);
Node1->left = Node2;
Node1->right = Node3;
Solution S1;
TreeNode* res = S1.inorderSuccessor(Node1, Node2);
std::cout << res->val << std::endl;
return 0;
}
3--Serialization and deserialization of binary tree
Main idea: