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来递归。代码如下:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSameTree(TreeNode *p, TreeNode *q) {
        if (!p && !q) return true;
        if ((p && !q) || (!p && q) || (p->val != q->val)) return false;
        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
    }
};
时间: 2024-08-25 00:31:04

Same Tree 判断相同树的相关文章

Leetcode:Symmetric Tree 判断对称树

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 解题分析: 二叉树递归,始终是第一颗二叉树的左子树和第二颗二叉树的右

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 Same Tree (判断相同树)

题意:如题 思路:递归解决,同判断对称树的原理差不多.先保证当前两个结点是相等的,再递归保证两左结点是相等的,再递归保证右结点是相等的. 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 *

【LeetCode】Symmetric Tree 判断一棵树是否是镜像的

题目:Symmetric Tree <span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树 * 思路:判断一棵树是否对称,1.有左子树就要有右子树 * 2.除根节点外对称节点值要相同 * 注意:对称后就是左子树的左节点和右子树的右节点比较 * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; *

【LeetCode-面试算法经典-Java实现】【101-Symmetric Tree(对称树)】

[101-Symmetric Tree(对称树)] [LeetCode-面试算法经典-Java实现][所有题目目录索引] 原题 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:

EasyUI Tree判断节点是否是叶

方法1:  $('#domaincatalog').tree('isLeaf', node.target); 返回true或false ,true表示是叶节点, false即不是 方法2:官方文档中:看到每个节点都有一些属性,其中一个是state,我们就通过这个state来判断.state有两个值 open和closed表示当前节点 打开和关闭了树的状态.当state等于undefined的时候就表示当前节点是Leaf 叶了. 在tree的onclick事件上添加判断代码如下 $("#domai

POJ-2201-Cartesian Tree(笛卡尔树)

Description Let us consider a special type of a binary search tree, called a cartesian tree. Recall that a binary search tree is a rooted ordered binary tree, such that for its every node x the following condition is satisfied: each node in its left

编程判断一个树是完全二叉树(使用层次遍历实现)

完全二叉树:一棵具有N个节点的二叉树的结构与满二叉树的前N个节点的结构相同 如何判断一个树是完全二叉树 可以使用层序遍历,只需2个步骤 第一步:如果遍历到一个节点只有右子树没有左子树,则不是完全二叉树 第二部:如果遍历到一个节点只有左子树,那么后面遍历到的节点必须是叶子节点,否则也不是完全二叉树 排除以上两种情况,则树是完全二叉树 核心代码: //层序遍历 int LayerOrder(BiTreeNode *head) { bool flag=0; LQueue Q; Initiate_Que

15-- 输入两个二叉树A和B,判断B树是否包含于A树。

// // main.cpp // subStructureInTree // // Created by Hugo Cao on 15/7/10. // Copyright (c) 2015年 Hugo Cao . All rights reserved. // /* 问题描述: 输入两个二叉树A和B,判断B树是否包含于A树. 查找B子树是否在A中, 思路:(1)首先寻找B根结点,是否在A中, (2)如果在,就查看是否A树包含B树 思考如何遍历A树寻找更结点,因为只有前序遍历可以最先找到根节点