Tip: after the article is written, the directory can be generated automatically. Please refer to the help document on the right for how to generate it
Classification of binary tree
Full binary tree: if a binary tree has only nodes with degree 0 and degree 2, and the nodes with degree 0 are on the same layer, the binary tree is a full binary tree.
Complete binary tree: in a complete binary tree, except that the bottom node may not be filled, the number of nodes in each layer reaches the maximum, and the nodes in the lowest layer are concentrated in several positions on the leftmost side of the layer. If the bottom layer is layer h, the layer contains 1~ 2^h -1 nodes.
Binary search tree: a binary tree with values. It is an ordered tree. If its left subtree is not empty, the values of all nodes on the left subtree are less than the values of its root node; If its right subtree is not empty, the values of all nodes on the right subtree are greater than those of its root node; Its left and right subtrees are also binary sort trees.
Balanced binary search tree: also known as AVL (Adelson velsky and Landis) tree, it is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, and both the left and right subtrees are a balanced binary tree.
Traversal of binary tree
1. Recursive method
2. Iterative method
**3. Sequence traversal
Leetcode 102, 107 questions: print sequence (reverse sequence of sequence)
class Solutions{ public: vector<vector<int>> levelOrder(TreeNode*root){ vector<vector<int>> result;//Store results queue<TreeNode*> que; if(root != NULL) que.push(root); while(!que.empty()){ int size = que.size(); vector<int> vec; for(int i=0;i<size;i++){ TreeNode*node = que.front(); vec.push_back(node->val); que.pop(); if(node->left) que.push(node->left); if(node->right) que.push(node->right); } result.push_back(vec); } //reverse(result.begin(),result.end()); //107 questions return result; } };
Leetcode 199 question: right view
class Solution{ public: vector<int> rightSideView(TreeNode* root){ vector<int> result;//Store results queue<TreeNode*> que; if(root != NULL) que.push(root); while(!que.empty()){ int size = que.size(); for(int i=0;i<size;i++){ TreeNode*node = que.front(); if(i == size-1) result.push_back(node->val);//Prints the elements indexed at the end of each row que.pop(); if(node->left) que.push(node->left); if(node->right) que.push(node->right); } } return result; } };
Leetcode 637 question: return the average value of each layer
class Solution{ public: vector<int> rightSideView(TreeNode* root){ vector<double> result;//Store results queue<TreeNode*> que; if(root != NULL) que.push(root); while(!que.empty()){ int size = que.size(); double sum = 0; for(int i=0;i<size;i++){ TreeNode *node = que.front(); sum += node->val;//Prints the elements indexed at the end of each row que.pop(); if(node->left) que.push(node->left); if(node->right) que.push(node->right); } result.push_back(sum/size); } return result; } };
Leetcode 429 question: return all of each layer, including children
class Solution{ public: vector<vector<int>> levelOrder(Node* root){ vector<vector<int>> result;//Store results queue<Node*> que; if(root != NULL) que.push(root); while(!que.empty()){ int size = que.size(); vector<int> vec; for(int i=0;i<size;i++){ Node *node = que.front(); vec.push_back(node->val); que.pop(); for(int i = 0;i<node->children.size();i++){ if(node->children[i]) que.push(node->children[i]); } } result.push_back(vec); } return result; } };
Leetcode 515 question: return the max of each layer
class Solution { public: vector<int> largestValues(TreeNode* root) { vector<int> result; queue<TreeNode*> que; if (root != NULL) que.push(root); while(!que.empty()){ int size = que.size(); vector<int> vec; int max_val = INT_MIN; for(int i= 0;i<size;i++){ TreeNode*node = que.front(); vec.push_back(node->val); que.pop(); max_val = node->val>max_val ? node->val :max_val; if(node->right) que.push(node->right); if(node->left) que.push(node->left); } result.push_back(max_val); } return result; } };
Leetcode 116 (117 exactly the same) question: return to the rightmost of each layer
class Solution { public: Node* connect(Node* root) { queue<Node*> que; if(root!=NULL) que.push(root); while(!que.empty()){ int size = que.size(); for(int i = 0;i<size;i++){ Node *cur = que.front(); que.pop(); if(i==size-1) cur->next = NULL; else cur->next = que.front(); if(cur->left) que.push(cur->left); if(cur->right) que.push(cur->right); } } return root;
Leetcode 104 question: return the maximum depth (that is, the number of layers)
class Solution { public: Node* connect(Node* root) { queue<Node*> que; if(root!=NULL) que.push(root); while(!que.empty()){ int size = que.size(); for(int i = 0;i<size;i++){ Node *cur = que.front(); que.pop(); if(i==size-1) cur->next = NULL; else cur->next = que.front(); if(cur->left) que.push(cur->left); if(cur->right) que.push(cur->right); } } return root;
summary
Generally speaking, it is a set of templates. Remember the most basic sequence template of question 102, and the rest can be changed slightly.
Reference: Code Capriccio https://programmercarl.com/0101.%E5%AF%B9%E7%A7%B0%E4%BA%8C%E5%8F%89%E6%A0%91.html