原题网址:https://www.lintcode.com/problem/same-tree/description

描述

检查两棵二叉树是否等价。等价的意思是说,首先两棵二叉树必须拥有相同的结构,并且每个对应位置上的节点上的数都相等。

您在真实的面试中是否遇到过这个题?  是

样例

    1             1
/ \ / \
2 2 and 2 2
/ /
4 4

就是两棵等价的二叉树。

    1             1
/ \ / \
2 3 and 2 3
/ \
4 4

就不是等价的。

思路:采用前序遍历判断。

a和b都为NULL,return true;

a和b只有其中一个为NULL,return false;

如果节点值不相等,return false;

递归判断左右子树,如果左右子树都相等,返回true;否则,返回false;

AC代码:

/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/ class Solution {
public:
/**
* @param a: the root of binary tree a.
* @param b: the root of binary tree b.
* @return: true if they are identical, or false.
*/
bool isIdentical(TreeNode * a, TreeNode * b) {
// write your code here
if (a==NULL&&b==NULL)
{
return true;
}
if (a==NULL||b==NULL)
{
return false;
}
if (a->val!=b->val)
{
return false;
}
bool x=isIdentical(a->left,b->left);
bool y=isIdentical(a->right,b->right);
return x&&y; }
};

PS:

初始代码直接递归左右子树,然后返回true,如下:

isIdentical(a->left,b->left);
isIdentical(a->right,b->right);
return true;

这种代码没有层层返回结果,判断左右子树是否都相同(即函数返回值是否都为true),所以导致只要根节点值相同就返回true,结果错误。

【若在某个非根节点a和b值不相同, 该层函数结果是false,但是没有反馈出去。】

其他参考:

https://www.cnblogs.com/grandyang/p/4053384.html

469 Same Tree的更多相关文章

  1. Lintcode469-Same Tree-Easy

    469. Same Tree Check if two binary trees are identical. Identical means the two binary trees have th ...

  2. 编译安装tree命令

    查看当前的tree [12:33:33 root@C8[ ~]#rpm -qi tree Name : tree Version : 1.7.0 Release : 15.el8 Architectu ...

  3. [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法

    二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...

  4. SAP CRM 树视图(TREE VIEW)

    树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...

  5. 无限分级和tree结构数据增删改【提供Demo下载】

    无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...

  6. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  7. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  8. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  9. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

随机推荐

  1. c&c++MFC 调用 js 函数代码

    调用函数代码和示例   Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlight ...

  2. ECMAScript 6中的Set和Map数据结构

    一.Set 基本用法: Set本身是一个构造函数,用来生成Set数据结构.Set函数可以接受一个数组作为参数用来初始化. const arr = new Set([2,2,3,3,4,4,5,8]); ...

  3. 8种形式的Android Dialog使用举例

    在Android开发中,我们经常会需要在Android界面上弹出一些对话框,比如询问用户或者让用户选择.这些功能我们叫它Android Dialog对话框,在我们使用Android的过程中,我归纳了一 ...

  4. 不能scp到本地mac,mac打开ssh服务

    设置->共享->远程登录->所有用户

  5. 在centos 6.9 x64下安装code::blocks步骤

    1.yum groupinstall "Development tools" 2.yum install gtk2* 3.安装wxWidgets 下载地址:https://www. ...

  6. USACO 2003 Fall Orange Cow Exhibition /// 负数01背包 oj22829

    题目大意: 输入n 接下来n行 每行输入 a b 输出n行中 a+b总和最大的同时满足 所有a总和>=0所有b总和>=0的值 负数的01背包应该反过来 w[i]为正数时 需要从大往小推 即 ...

  7. [WPF自定义控件]?Window(窗体)的UI元素及行为

    原文:[WPF自定义控件]?Window(窗体)的UI元素及行为 1. 前言 本来打算写一篇<自定义Window>的文章,但写着写着发觉内容太多,所以还是把使用WindowChrome自定 ...

  8. mac brew 安装 php 环境

    548  brew search php 549  brew tap homebrew/dupes 550  brew tap josegonzalez/homebrew-php 551  brew ...

  9. leetcode-213-打家劫舍二

    题目描述: 方法一: class Solution(object): def rob(self, nums): """ :type nums: List[int] :rt ...

  10. 2-sat——hdu3062基础

    hdu就是会卡cin,, 另一种建模方式,把点i拆成i*2,i*2+1,有时候这样会比较简单 #include<bits/stdc++.h> using namespace std; #d ...