isSameTree

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;
		 if(isSameTree(p->right,q->right)==false)
		 	return false;
		if(isSameTree(p->left,q->left)==false)
		 	return false;
        return true;
    }
};
时间: 2024-07-29 06:08:50

isSameTree的相关文章

二叉树的比较 ~ isSameTree算法实现

字节三面的一道面试题,判断是否是相同二叉树 1 var isSameTree = function (p, q) { 2 if (p === null && q === null) return true; 3 if (p === null && q !== null) return false; 4 if (p !== null && q === null) return false; 5 if (p.val !== q.val) return false

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. 1 /** 2 * Definition for a binary tree node. 3 * public class Tree

LeetCode刷题之三:判断两个二叉树是否相同

题目为: 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. 解题思路:这种题目也是递归操作简单 代码为: /** * Definition for binary tree * pub

【LeetCode】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. 思路: 考察递归.判断两棵树相等,只要递归判断两棵树的结构和值.所以遇到一个指针为空的时候,另一个指针一定要为空.不为空的时

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. 思路同比较二叉树是否对称,使用递归的方法 1 /** 2 * Definition for binary tree 3 * stru

[LeetCode] NO. 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. [题目解析]  二叉树的问题,转化成当前节点与左子树,右子树问题.注意一些临界条件的判断. /** * Definitio

100. Same Tree

1. 问题描述 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. Subscribe to see which companies asked this question Tags:

【6_100】Same Tree

Same Tree Total Accepted: 97481 Total Submissions: 230752 Difficulty: Easy 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 sam

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