[Leetcode] 100.Same Tree

来源:互联网 发布:在线字体生成网站源码 编辑:程序博客网 时间:2024/06/07 22:58

[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.

思路

同时遍历一遍树,发现任何一个节点不相等则两棵树不相等

/** * 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 == NULL && q == NULL) return true;        if((p == NULL) ^ (q == NULL)) return false;        if(p->val != q->val) return false;        return isSameTree(p->left,q->left) && isSameTree(p->right,q->right);    }};
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 颈椎颈椎 看颈椎哪里好 颈椎拼音 颈椎图像 神经颈椎 颈椎读音 颈椎是什么科 全国颈椎医院 看颈椎的医院 后颈椎 腰椎间盘牵引 颈椎疼痛是怎么回事 关于颈椎 颈椎间盘突出原因 劲歌金曲 劲歌香江 劲歌热舞 ktv劲歌金曲 劲歌贵公子 劲歌 劲歌金曲歌词 劲歌金曲颁奖典礼 劲歌金曲有哪些歌 劲歌金曲4有哪些歌 林海峰劲歌金曲歌词 江若琳 劲歌金曲 粤语劲歌金曲 劲歌金曲2 mv 林海峰劲歌金曲 劲歌金曲情歌王 2008年劲歌金曲 劲歌金曲搞笑版歌词 劲歌金曲 twins 劲歌金曲2国语 2008劲歌金曲颁奖礼 劲歌金曲3有哪些歌 荣蓉2018劲歌美舞慢摇 劲歌金曲下载 经典劲歌金曲 英文劲歌金曲 劲歌金曲3演唱会