100. Same Tree
生活随笔
收集整理的這篇文章主要介紹了
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.
?
遞歸遍歷左子樹和右子樹
/*** 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 != NULL && q == NULL) return false;if (p->val == q->val) {bool x = isSameTree(p->left, q->left);bool y = isSameTree(p->right, q->right);return x&&y;}return false;} };?
轉載于:https://www.cnblogs.com/pk28/p/7218111.html
總結
以上是生活随笔為你收集整理的100. Same Tree的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: zabbix监控系列(5)之通过trap
- 下一篇: [POI2007]POW-The Flo