leetcode || 100、Same Tree

problem:

Given two binary trees, write a function to check if they are equal or not.

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

Hide Tags

Tree Depth-first
Search

题意:检查两棵 二叉树是否相同 ,二叉树相同定义:1、结构相同 2、每个节点的数值相等

thinking:

使用深搜,一边检查结构一边检查数值

code:

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);
      }
  };
时间: 2024-08-28 04:03:42

leetcode || 100、Same Tree的相关文章

LeetCode(100)题解--Same Tree

https://leetcode.com/problems/same-tree/ 题目: Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 思路:  DFS AC代码: 1.递归 1

LeetCode 100: Same Tree

Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 分析: 题目要求判断两棵树是否相同,首先判断根节点是否相同,如果根节点相同,分别判断左.右子树是否相同. 代码如下: /** * D

leetcode || 144、Binary Tree Preorder Traversal

problem: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? Hide Tags Tree Stack 题意:非递归前序遍历二叉树 t

leetcode || 103、Binary Tree Zigzag Level Order Traversal

problem: Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 retur

leetcode || 94、Binary Tree Inorder Traversal

problem: Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? confused what "{1,#,2,3}" means?

leetcode || 107、Binary Tree Level Order Traversal II

problem: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level or

leetcode || 101、Symmetric Tree

problem: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For example, this binary tree is symmetric: 1 / 2 2 / \ / 3 4 4 3 But the following is not: 1 / 2 2 \ 3 3 Note: Bonus points if you could solve it

leetcode || 102、Binary Tree Level Order Traversal

problem: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7

leetcode || 145、Binary Tree Postorder Traversal

problem: Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? Hide Tags Tree Stack 题意:非递归后续遍历二叉树