Code Caprice Algorithm Training Camp 15th Day 14 | Theoretical basis, recursive traversal, iterative traversal, unified iteration

theoretical basis 

Types of Binary Trees

Need to understand the types of binary trees, storage methods, traversal methods and the definition of binary trees

full binary tree

Full binary tree: If a binary tree has only degree 0 nodes and degree 2 nodes, and the degree 0 nodes are on the same level, then the binary tree is a full binary tree.

complete binary tree 

The definition of a complete binary tree is as follows: In a complete binary tree, except for the bottom node that may not be filled, the number of nodes in each layer reaches the maximum value, and the nodes in the bottom layer are all concentrated in the leftmost positions of the layer. If the bottom layer is the hth layer, then this layer contains 1~ 2^(h-1) nodes.

The priority queue is actually a heap, and the heap is a complete binary tree, while ensuring the order relationship of parent and child nodes. 

binary search tree

A binary search tree has values, and a binary search tree is an ordered tree .

  • If its left subtree is not empty, the values ​​of all nodes on the left subtree are less than the value of its root node;
  • If its right subtree is not empty, the values ​​of all nodes on the right subtree are greater than the value of its root node;
  • Its left and right subtrees are also binary sorted trees

Balanced Binary Search Tree

Balanced binary search tree: also known as AVL (Adelson-Velsky and Landis) tree, and has the following properties: it is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, and the left and right Both subtrees are a balanced binary tree.

In C++, the underlying implementations of map, set, multimap, and multiset are all balanced binary search trees, so the time complexity of adding and deleting operations of map and set is logn. Note that I did not mention unordered_map, unordered_set, unordered_map, and unordered_set. Greek table. 

Binary tree storage method

Binary trees can be stored in chains or sequentially. Linked storage uses pointers, and sequential storage uses arrays. Sequentially stored elements are continuously distributed in memory, while chained storage uses pointers to connect nodes distributed at various addresses in series.

The array storage method is as follows:  if the array subscript of the parent node is i, then its left child is i * 2 + 1, and its right child is i * 2 + 2.

How to traverse a binary tree

The order of intermediate nodes is the so-called traversal mode

  • Preorder traversal: middle left
  • Inorder traversal: left center right
  • Post-order traversal: left, right, middle

Definition of binary tree 

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

recursive traversal

Once you enter the recursion, it is as deep as the sea.

The trilogy of recursion:
①Determine the parameters and return value of the recursive function
②Determine the termination condition
③Determine the logic of single-level recursion

Preorder traversal: middle left

Referring to the preorder traversal, the traversal process is as follows:
① Determine the parameters and return value of the recursive function

void traversal(TreeNode* cur, vector<int>& vec)

② Determine the termination conditions

if (cur == NULL) return;

③ Determine the logic of single-level recursion

vec.push_back(cur->val);    // 中
traversal(cur->left, vec);  // 左
traversal(cur->right, vec); // 右

In-order traversal: left-middle right 
post-order traversal: left-middle

Preorder traversal topic link: 144. Preorder traversal button

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void pre(TreeNode* use,vector<int>& abc)
    {
        if(use==nullptr) return;
        abc.push_back(use->val);
        pre(use->left,abc);
        pre(use->right,abc);

    }
    vector<int> preorderTraversal(TreeNode* root) {
        vector<int> result;
        pre(root, result);
        return result;

    }
};

Inorder traversal topic link: 94. Inorder traversal button

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void mid(TreeNode* use,vector<int>& kv)
    {
        if(use==nullptr)
        {
            return;
        }
        mid(use->left,kv);
        kv.push_back(use->val);
        mid(use->right,kv);

    }
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> result;
        mid(root,result);
        return result;

    }
};

 Post-order traversal topic link: 145. Post-order traversal button

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    void back1(TreeNode* use,vector<int>& kv)
    {
        if(use==nullptr)
        {
            return;
        }
        back1(use->left,kv);
       
        back1(use->right,kv);
        kv.push_back(use->val);

    }
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> result;
        back1(root,result);
        return result;

    }
};

 

Guess you like

Origin blog.csdn.net/m0_47489229/article/details/131082197