博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode]Symmetric Tree
阅读量:7016 次
发布时间:2019-06-28

本文共 1043 字,大约阅读时间需要 3 分钟。

Symmetric Tree

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

For example, this binary tree is symmetric:

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

 

But the following is not:

1   / \  2   2   \   \   3    3

Note:

Bonus points if you could solve it both recursively and iteratively.

confused what "{1,#,2,3}" means? 

 

DFS。

1 /** 2  * Definition for a binary tree node. 3  * struct TreeNode { 4  *     int val; 5  *     TreeNode *left; 6  *     TreeNode *right; 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8  * }; 9  */10 class Solution {11 public:12     bool check(TreeNode* left,TreeNode* right)13     {14         if(!left && !right) return true;15         if(!left || !right) return false;16         return (left->val==right->val)&&check(left->left,right->right)&&check(left->right,right->left);17     }18     bool isSymmetric(TreeNode* root) {19         if(!root) return true;20         return check(root->left,root->right);21     }22 };

 

转载于:https://www.cnblogs.com/Sean-le/p/4805952.html

你可能感兴趣的文章
[转]深入浅出Java设计模式之备忘录模式
查看>>
抽象类到底是个什么玩意
查看>>
Windows Mobile 消除安装程序时显示“此程序来自未知发行者…“提示的方法
查看>>
正则表达式周二挑战赛 - 第十一周
查看>>
How many people have ipad II(数学)
查看>>
自适应对话框的背景位图&限制大小的对话框
查看>>
分享:centos daemon make and install
查看>>
javascript对话框
查看>>
机器学习各类工具weka、scikit-learn等各项指标的对比
查看>>
安全编程-c++野指针和内存泄漏
查看>>
2012时光之末,2013时光之初
查看>>
大规模web服务开发技术(转)
查看>>
vc6.0执行程序正确而debug版和release版运行错误
查看>>
淘宝褚霸谈做技术的心态
查看>>
Java Hibernate 二级缓存配置及缓存的统计策略
查看>>
【sas notes】sas9.2安装
查看>>
jsp页面修改后保存无反映,后台也没有执行到代码。
查看>>
Java 编程下泛型的内部原理
查看>>
倒排索引 - doudoubluesky的日志 - 网易博客
查看>>
Probe how does your PGA consume
查看>>