Binary tree OJ (1) Maximum depth of binary tree && binary search tree and doubly linked list && symmetrical binary tree

The maximum depth of the binary tree

The path that sums to a certain value in the binary tree (1)

Binary Search Tree and Doubly Linked List

Symmetrical Binary Tree


The maximum depth of the binary tree

describe

Find the maximum depth of a given binary tree,

Depth refers to the number of nodes on the path from the root node of the tree to any leaf node.

The maximum depth is the maximum value of the depths of all leaf nodes.

(Note: A leaf node refers to a node without child nodes.)

[recursion]

class Solution {
public:
    int maxDepth(TreeNode* root) {
        // write code here
        if(root==nullptr)
        return 0;
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
        return left>right?left+1:right+1;
    }
};

[Non-recursive] Layer order traversal (using queues to store nodes)

class Solution {
public:
    int maxDepth(TreeNode* root) {
        // write code here
        if(root == nullptr)
            return 0;
        int res = 0;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty())
        {
            int size = q.size();
            while(size--)
            {
                TreeNode* cur = q.front();
                q.pop();
                if(cur->left) q.push(cur->left);
                if(cur->right) q.push(cur->right);
            }
            res++;
        }
        return res;
    }
};

 

The path that sums to a certain value in the binary tree (1)

describe

Given a binary tree root and a value sum, determine whether there is a path whose sum of node values ​​from the root node to the leaf node is equal to sum.

1. The path of this question is defined as the node from the root node of the tree down to the leaf node

2. A leaf node refers to a node without child nodes

3. The path can only go from parent node to child node, not from child node to parent node

4. The total number of nodes is n


For example:
given the following binary tree, sum=22 sum=22,


Returns true because there is a path 5→4→11→25→4→11→2 and the sum of the node values ​​is 22

class Solution {
public:
    bool flag = false;
    void dfs(TreeNode* root, int sum)
    {
        if(root==nullptr)return;
        sum-=root->val;
        if(sum==0 && root->left==nullptr && root->right==nullptr)
        {
            flag = true;    // 如果为根节点并且sum==0那么存在路径
            return;
        }
        dfs(root->left, sum);
        dfs(root->right, sum);
    }
    bool hasPathSum(TreeNode* root, int sum) {
        dfs(root, sum);
        return flag;
    }
};

 

Binary Search Tree and Doubly Linked List

Input a binary search tree and convert the binary search tree into a sorted doubly linked list. As shown below

Data range: the number of nodes in the input binary tree 0≤n≤10000≤n≤1000, the value of each node in the binary tree 0≤val≤10000≤val≤1000
requirements: space complexity O(1)O(1) (that is, in the original Tree operation), time complexity O(n)O(n)

Notice:

1. It is required that no new nodes can be created, only the pointing of the node pointers in the tree can be adjusted. After the transformation is completed, the left pointer of the node in the tree needs to point to the predecessor, and the right pointer of the node in the tree needs to point to the successor 2.
Return the pointer to the first node in the linked list
3. The TreeNode returned by the function has left and right pointers, which can be seen in fact into a data structure of a doubly linked list

4. You don't need to output the doubly linked list, the program will automatically print out according to your return value

 

class Solution {
public:
	vector<TreeNode*> res;
	void Inoder(TreeNode* root)
	{
		if(root == NULL)
		return;
		Inoder(root->left);
		res.push_back(root);
		Inoder(root->right);
	}
    TreeNode* Convert(TreeNode* pRootOfTree) {
        if(pRootOfTree==NULL)
		return NULL;
		Inoder(pRootOfTree);
		for(int i = 0; i < res.size()-1; ++i)
		{
			res[i]->right = res[i+1];
			res[i+1]->left = res[i];
		}
		return res[0];
    }
};

Symmetrical Binary Tree

describe

Given a binary tree, determine whether it is a mirror image of itself (ie: whether it is symmetrical)
For example: The following binary tree is symmetrical


The following binary tree is asymmetrical.
 

Data range: the number of nodes satisfies 0≤n≤10000≤n≤1000, the value on the node satisfies ∣val∣≤1000∣val∣≤1000

Requirements: space complexity O(n)O(n), time complexity O(n)O(n)

Remark:

You can solve this problem in two ways recursive and iterative

 [recursive solution]

class Solution {
public:
    bool recursion(TreeNode* p, TreeNode* q)
    {
        if(p==nullptr && q==nullptr)
            return true;
        else if(p==nullptr || q==nullptr)
            return false;
        else if(q->val != p->val)
            return false;
        return recursion(p->left, q->right) && 
               recursion(p->right, q->left);
    }
    bool isSymmetrical(TreeNode* root) {
        if(root==nullptr)return true;
        return recursion(root, root);
    }
};

[non-recursive]

class Solution {
public:
    bool isSymmetrical(TreeNode* root) {
        if(root==nullptr)return true;
        queue<TreeNode*> q1;
        queue<TreeNode*> q2;
        q1.push(root->left);
        q2.push(root->right);
        while(!q1.empty() && !q2.empty())
        {
            TreeNode* left = q1.front();
            TreeNode* right = q2.front();
            q1.pop();
            q2.pop();
            if(left==nullptr && right==nullptr)
                continue;
            if(left==nullptr || right==nullptr || left->val != right->val)
                return false;
            q1.push(left->left);
            q1.push(left->right);
            q2.push(right->right);
            q2.push(right->left);
        }
        return true;
    }
};

Guess you like

Origin blog.csdn.net/weixin_66151870/article/details/129112913