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 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSameTree(TreeNode* p, TreeNode* q) {
13         if (p==NULL && q==NULL)
14             return true;
15         else if(p!=NULL&&q!=NULL){
16         bool ju;
17         if(p->val==q->val)
18             ju=true;
19         else
20             return false;
21         if (p->left==NULL && q->left==NULL)
22             ;
23         else if(p->left!=NULL && q->left!=NULL)
24             ju=ju&&isSameTree(p->left,q->left);
25         else
26             return false;
27         if (p->right==NULL && q->right==NULL)
28             ;
29         else if(p->right!=NULL && q->right!=NULL)
30             ju=ju&&isSameTree(p->right,q->right);
31         else
32             return false;
33         return ju;
34         }
35         else
36             return false;
37     }
38 };

2.非递归

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isSameTree(TreeNode* p, TreeNode* q) {
13         if(p==NULL&&q==NULL)
14             return true;
15         else if(p!=NULL && q!=NULL){
16             stack<TreeNode*> ms1,ms2;
17             ms1.push(p);
18             ms2.push(q);
19             TreeNode* p1,*q1;
20             while(!ms1.empty()){
21                 p1=ms1.top();
22                 q1=ms2.top();
23                 if(p1->val!=q1->val)
24                     return false;
25                 else{
26                     ms1.pop();
27                     ms2.pop();
28                     if(p1->right==NULL&&q1->right==NULL)
29                         ;
30                     else if(p1->right!=NULL&&q1->right!=NULL){
31                         ms1.push(p1->right);
32                         ms2.push(q1->right);
33                     }
34                     else
35                         return false;
36                     if(p1->left==NULL&&q1->left==NULL)
37                         ;
38                     else if(p1->left!=NULL&&q1->left!=NULL){
39                         ms1.push(p1->left);
40                         ms2.push(q1->left);
41                     }
42                     else
43                         return false;
44                     }
45                 }
46                 return true;
47             }
48         else
49             return false;
50     }
51 };
时间: 2024-12-14 18:45:57

LeetCode(100)题解--Same Tree的相关文章

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 题意:检查两棵 二叉树是否相同 ,二叉树相同定

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 题解]: Binary Tree Preorder Traversal

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? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

[LeetCode 题解]: Symmetric Tree

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 both recu

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: 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? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode题解:Tree Level Order Traversal II (二叉树的层序遍历 2)

题目: 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 order tr

leetcode 题解:Binary Tree Level Order Traversal (二叉树的层序遍历)

题目: 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] ] co

LeetCode: Validata Binary Search Tree

LeetCode: Validata Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree o

LeetCode: Recover Binary Search Tree

LeetCode: Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing its structure. Note: A solution using O(n) space is pretty straight forward. Could you devise a constant space s