[Data Structure] Binary Tree-OJ

  Yan-yingjie's homepage

Awareness of the past, not remonstrance, knowing the future, can be pursued  

C++ programmer, 2024 electronic information graduate student


Table of contents

1. The same tree

2. Flip the binary tree

Three, single-valued binary tree

Fourth, the maximum depth of the binary tree


1. The same tree

        

        Given the roots of two binary trees pand q, 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:

输入:p = [1,2], q = [1,null,2]
输出:false

 Example 2:

        

输入:p = [1,2,1], q = [1,1,2]
输出:false

Example 3:

        

输入:p = [1,2,1], q = [1,1,2]
输出:false

Code analysis:

        

bool isSameTree(struct TreeNode* p, struct 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); 
}

Drawing analysis:

        

2. Flip the binary tree

        Given the root of a binary tree root, flip the tree and return its root.

Example 1:

输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]

Example 2:

输入:root = [2,1,3]
输出:[2,3,1]

 Example 3:

输入:root = []
输出:[]

Code analysis:

struct TreeNode* invertTree(struct TreeNode* root){
    if(root == NULL){
        return NULL;
    }
    struct TreeNode* left = invertTree(root->left);
    struct TreeNode* right = invertTree(root->right);
    root->left = right;
    root->right = tmp;
    return root;
}

Image analysis:

Three, single-valued binary tree

A binary tree is a single- valued binary tree         if every node of the tree has the same value .

        Returns only if the given tree is a single-valued binary tree  true; otherwise false.

        Example 1:

输入:[1,1,1,1,1,null,1]
输出:true

        Example 2:

输入:[2,2,2,5,2]
输出:false

        Code analysis:

                


bool isUnivalTree(struct TreeNode* root){
  if(root == NULL)
  {
      return true;
  }
  if(root->left&&root->left->val != root->val)
  {
      return false;
  }
  if(root->right&&root->right->val!=root->val)
  {
      return false;
  }
  return isUnivalTree(root->left)&&isUnivalTree(root->right);
}

        Image analysis:

                

Fourth, the maximum depth of the binary tree

        Given a binary tree, find its maximum depth.

        The depth of a binary tree is the number of nodes on the longest path from the root node to the furthest leaf node.

        Explanation:  A leaf node refers to a node without child nodes.

        Example:


     Given a binary tree [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

    Return its maximum depth of 3 .

 Code analysis:

        

int maxDepth(struct TreeNode* root){
    if(root==NULL)
    {
        return 0;
    }
    int leftHeight = maxDepth(root->left);
    int rightHeight = maxDepth(root->right);
    return leftHeight > rightHeight ? leftHeight + 1:rightHeight+1;
}

 Image analysis:

Guess you like

Origin blog.csdn.net/m0_73367097/article/details/130308231