您好,登錄后才能下訂單哦!
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) { bool childResult; if( NULL == p && NULL == q) return true; if( NULL != p && NULL != q && p->val == q->val) { return childResult = isSameTree(p->left,q->left) && isSameTree(p->right,q->right); } return false; } };
2016-08-07 00:01:38
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。