100. Same Tree

来源:互联网 发布:太平天国失败 知乎 编辑:程序博客网 时间:2024/06/08 03:30

题目

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

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

Example 1:

Input:     1         1          / \       / \         2   3     2   3        [1,2,3],   [1,2,3]

Output: true
Example 2:

Input:     1         1          /           \         2             2        [1,2],     [1,null,2]

Output: false
Example 3:

Input:     1         1          / \       / \         2   1     1   2        [1,2,1],   [1,1,2]

Output: false

题意

判断两棵树是否完全相同

分析

DFS递归实现

实现

/** * Definition for a binary tree node. * 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 && p->val == q->val) {            return isSameTree(p->left, q->left)                    && isSameTree(p->right, q->right);        } else if (!p && !q) return true;        return false;    }};
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 悦达起亚k3s价格 悦达k2怎么样 悦达起亚福瑞达 悦达起亚k2两厢怎么样 悦达起亚智跑怎么样 悦达起亚两厢 悦达起亚所有车型 悦达起亚索兰托 悦达起亚商务 悦达起亚千里马怎么样 悦达起亚k2好不好 悦达起亚锐欧配件 悦达起亚kx3白色 悦达起亚秀尔报价 悦达起亚k2三厢价格 悦达起亚k5 报价 悦达起亚福瑞迪图片 悦达起亚k3s报价 悦达起亚sport 600805悦达投资股吧 悦达起亚欧菲莱斯 悦达起亚k5三厢 风悦达起亚kx3 悦达起亚k2两箱 悦达起亚欧风 悦达起亚是哪国品牌 悦达起亚k2香槟金 2015款悦达起亚k5 东凤悦达起亚kx3 2014款悦达起亚k5 途悦 大众途悦 途悦大众 悦途出行 大众途悦suv报价 途悦价格及图片 悦途商务贵宾厅 上海大众途悦 涠洲岛途悦酒店 悦途出行会员卡价格表 悦途车载电脑