LeetCode[94/100/101] Inorder traversal of binary tree, same tree, symmetric binary tree C/C++——Week 2 I

binary tree

1. The concept of binary tree

Binary tree : a finite set of n (n≥0) nodes, either empty (n=0), or consisting of a root node and two disjointand right subtrees called the root, respectively The binary tree composition of the tree .

Even if there is only one child, a strict distinction must be made between left and right . And binary tree and tree are two tree structures.

2. Basic form of binary tree

Empty tree, only root, left subtree is empty, right subtree is empty, neither left subtree nor right subtree is empty.

3. Binary tree traversal

Traversal : Travel the binary tree along a certain search path, and visit each node once and only once.

For nonlinear structure traversal, a linear sequence of nodes arranged in a certain order is obtained, so traversal can be regarded as a mapping method from nonlinear structure to linear structure .

There are two types of binary tree traversal: level traversal and recursive traversal .

Recursive traversal:
If it is limited to first left and then right , there are three binary tree traversal methods:
preorder: DLR, inorder: LDR, postorder: LRD (at this time, the binary tree consists of root node D, left subtree L, right subtree R constitute).

Hierarchical traversal: visit each node in the sequence numbered sequence of the binary tree.

94. Inorder traversal of a binary tree

Title description [simple]:

Given the root node root of a binary tree, return its inorder traversal.

Example 1:
Example 1

Input: root = [1,null,2,3]
Output: [1,3,2]

Thoughts [recursion]:

First of all, let's look at the definition of in-order traversal:
if the binary tree is empty, the null operation returns;
otherwise:
① In-order traversal of the left subtree of the root node;
② Access to the root node;
③ In-order traversal of the right child of the root node Tree.
According to its nature, we need to use recursion to apply it continuously, and it is easy to write its code.

C++ code:

/**
 * 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 inorder(TreeNode* root, vector<int>& r) {
    
    
         if(root!=NULL) {
    
    
             inorder(root->left, r);	//中根遍历左子树
             r.push_back(root->val);	//访问根,在尾部加入一个数据。
             inorder(root->right, r);	//中根遍历右子树
    }
    }
    vector<int> inorderTraversal(TreeNode* root) {
    
     //vector容器是一个模板类,可以存放任何类型的对象
        vector<int> r;
        inorder(root, r);
        return r;
    }
};

Time complexity: O(n), where n is the number of binary tree nodes. In the traversal of the binary tree, each node will be visited once and only once.

100. The same tree

Title description [simple]:

Given the root nodes p and q of two binary trees, write a function to check whether the two trees are the same.

Two trees are considered identical if they are structurally identical and the nodes have the same value.

Example 1:
1

Input: p = [1,3], q = [1,null,3]
Output: false

Idea [recursion]:

Judging whether two trees are the same is essentially traversing the two trees to judge whether the values ​​on each subtree are the same.
The easiest thing we can think of here is the recursive method.

The judgment structure of this question is the same:
first judge whether the root nodes of the two trees are the same, whether
the left subtree and the right subtree of the root nodes of the two trees are the same

The conditions for judging the left subtree and the right subtree are the same:
whether the left subtree of the left subtree is the same, whether the right subtree of the right subtree is the same, and keep going down until the result is returned.

The recursive condition and result of this question are:
when the traversed nodes of the two trees are empty at the same time, return true.
Returns false when one is empty and the other is not.
Returns false when both are non-null but have unequal values.
If they are all equal, start traversing backwards, and there will be results directly or all traversals will be completed.

C++ code:

/**
 * 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:
    bool isSameTree(TreeNode* p, TreeNode* q) {
    
    
        if(p == NULL && q == NULL) 
            return true;
        if(p == NULL || q == NULL) 
            return false;
        if(p->val != q->val) 
            return false;
        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
};

Time complexity: O(n) n is the number of nodes traversed.

101. Symmetric Binary Tree

Title description [simple]:

Given the root node root of a binary tree, check if it is axisymmetric.

Example 1:
3

Input: root = [1,2,2,3,4,4,3]
Output: true

Idea [recursion]:

The ideas of these questions are recursive, and the recursive thinking is understood step by step.

This question judges whether it is axisymmetric.
First judge the root node . If the root node is empty, then it is symmetrical.
If it is not empty, if the left subtree is symmetrical to the right subtree, it means it is symmetrical.

Then judging the symmetry of the left subtree and the right subtree
is to judge whether the left child of the left subtree is symmetric to the right child of the right subtree, and whether the right child of the left subtree is symmetric to the left child of the right subtree. It shows that the left subtree and the right subtree are symmetric.
This is where the idea of ​​recursion exists.

The recursive condition and result of this question are:
when the root node is empty , return true.
When the two nodes traversed down are both empty , return true.
When one of the two nodes traversed down is empty , return false.
Returns false when the values ​​of the two nodes traversed down are not equal .
The recursive condition of this question is almost the same as the previous one, but the recursive object is quite different.

The recursive object of this question:
the left child of the left node and the right child of the right node,
the right child of the left node and the left child of the right node

C++ code:

/**
 * 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:
    bool isSymmetric(TreeNode* root) {
    
    
        if(root == NULL){
    
    
        return true;
        }
    return symmetric(root->left,root->right);

    }
    bool symmetric(TreeNode* l,TreeNode* r){
    
    
        if(l == NULL && r== NULL){
    
    
        return true;
        }
        if(l == NULL || r == NULL || l->val != r -> val){
    
    
        return false;
        }
        return symmetric(l->left,r->right) && symmetric(l->right,r->left);

    }

};

Time complexity: O(n)

Guess you like

Origin blog.csdn.net/Lailalalala/article/details/126099078