国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

leetcode 101. Symmetric Tree

2019-11-08 02:57:13
字體:
供稿:網(wǎng)友

題目鏈接: https://leetcode.com/PRoblems/symmetric-tree/?tab=Description

題目描述:

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:

    1   / /  2   2 / / / /3  4 4  3

But the following [1,2,2,null,3,null,3] is not:

    1   / /  2   2   /   /   3    3

分別按照先左后右和先右后左的順序進行DFS,比較對應節(jié)點的值是否相等

代碼:

/** * 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 isSymmetric(TreeNode* root) {        if(root==NULL||(root->left==NULL&&root->right==NULL))            return 1;        else return DFS(root->left,root->right);    }    bool DFS(TreeNode* root1,TreeNode* root2)    {        if(root1==NULL&&root2==NULL)            return 1;        else if((root1==NULL&&root2!=NULL)||(root1!=NULL&&root2==NULL))            return 0;        else if(root1->val!=root2->val)            return 0;        else            return DFS(root1->left,root2->right)&&DFS(root1->right,root2->left);    }};


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 石屏县| 历史| 大厂| 丰顺县| 贵德县| 商丘市| 云霄县| 磐安县| 万荣县| 临洮县| 屯门区| 射洪县| 潜山县| 牡丹江市| 泌阳县| 石棉县| 南皮县| 莱州市| 霍州市| 东方市| 海晏县| 应用必备| 无为县| 霍山县| 台东市| 万盛区| 乐山市| 青阳县| 观塘区| 荥阳市| 台湾省| 大荔县| 孟州市| 永宁县| 连平县| 宣威市| 峡江县| 陈巴尔虎旗| 西吉县| 上高县| 清河县|