2017.11.3 LeetCode

来源:互联网 发布:java算法书籍推荐 编辑:程序博客网 时间:2024/06/03 18:51

101. Symmetric Tree

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:
这里写图片描述

题意: 判断一个树是否是对称的

分析: 直接dfs搜即可,注意的就是left 对应 right ,right 对应 left

参考函数

class Solution {public:    bool tag = true;    void dfs(TreeNode* root_l,TreeNode* root_r) {        if(!root_l && !root_r) return;        if(!root_l || !root_r) {            tag = false;return;        }        if(root_l->val != root_r->val) {            tag = false;return;        }        dfs(root_l->left,root_r->right);        dfs(root_l->right,root_r->left);    }    bool isSymmetric(TreeNode* root) {        if(!root) return tag;        dfs(root->left,root->right);        return tag;    }};
原创粉丝点击