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? > read more on how binary tree is serialized on OJ.

  OJ's Binary Tree Serialization:

  The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.

  Here's an example:

   1
/ \
2 3
/
4
\
5

  The above binary tree is serialized as "{1,2,3,#,#,4,#,#,5}".

  本题暂用递归解法,具体程序(4ms)如下:
 /**
* 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:
void updateLVec(TreeNode *left, vector<int>& lVec)
{
if(left)
{
lVec.push_back(left->val);
if(left->left || left->right)
{
if(left->left)
updateLVec(left->left, lVec);
else
lVec.push_back(INT_MIN);
if(left->right)
updateLVec(left->right, lVec);
else
lVec.push_back(INT_MIN);
}
}
} void updateRVec(TreeNode *right, vector<int>& rVec)
{
if(right)
{
rVec.push_back(right->val);
if(right->left || right->right)
{
if(right->right)
updateRVec(right->right, rVec);
else
rVec.push_back(INT_MIN);
if(right->left)
updateRVec(right->left, rVec);
else
rVec.push_back(INT_MIN);
}
}
} bool isSymmetric(TreeNode* root) {
if(!root || (!root->left && !root->right))
return true; if((!root->left && root->right) || (root->left && !root->right))
return false; vector<int> lVec, rVec;
updateLVec(root->left, lVec);
updateRVec(root->right, rVec); int szLVec = lVec.size();
int szRVec = rVec.size();
if(szLVec != szRVec)
return false; for(int i = ; i < szLVec; i++)
{
if(lVec[i] != rVec[i])
return false;
} return true;
}
};

  虽然最终解决了问题,但代码还是复杂了。别人的代码(4ms)真叫一个简单呐:

 bool isSymmetric(TreeNode *root)
{
if (!root) return true;
return isSymmetric(root->left, root->right);
}
bool isSymmetric(TreeNode *lt, TreeNode *rt)
{
if (!lt && !rt) return true;
if (lt && !rt || !lt && rt || lt->val != rt->val) return false;
return isSymmetric(lt->left, rt->right) && isSymmetric(lt->right, rt->left);
}

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.

  有了上一道题的基础,这道题就很简单了,具体程序(0ms)如下:

 /**
* 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 && !q)
return true;
return isSameTreeSub(p, q);
} bool isSameTreeSub(TreeNode *lt, TreeNode *rt)
{
if(!lt && !rt)
return true;
if((!lt && rt) || (lt && !rt) || (lt->val != rt->val))
return false;
return isSameTree(lt->left, rt->left) && isSameTree(lt->right, rt->right);
}
};
 

LeetCode之“树”:Symmetric Tree && Same Tree的更多相关文章

  1. LeetCode之“树”:Balanced Binary Tree

    题目链接 题目要求: Given a binary tree, determine if it is height-balanced. For this problem, a height-balan ...

  2. leetcode面试准备:Implement Trie (Prefix Tree)

    leetcode面试准备:Implement Trie (Prefix Tree) 1 题目 Implement a trie withinsert, search, and startsWith m ...

  3. leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree

    leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...

  4. [LeetCode] 366. Find Leaves of Binary Tree 找二叉树的叶节点

    Given a binary tree, find all leaves and then remove those leaves. Then repeat the previous steps un ...

  5. LeetCode & tree & binary tree

    LeetCode & tree & binary tree 树 & 二叉树 refs https://leetcode.com/problemset/all/?topicSlu ...

  6. 动态树之LCT(link-cut tree)讲解

    动态树是一类要求维护森林的连通性的题的总称,这类问题要求维护某个点到根的某些数据,支持树的切分,合并,以及对子树的某些操作.其中解决这一问题的某些简化版(不包括对子树的操作)的基础数据结构就是LCT( ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. leetcode算法: Find Bottom Left Tree Value

    leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...

  9. [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树

    [BZOJ3080]Minimum Variance Spanning Tree/[BZOJ3754]Tree之最小方差树 题目大意: 给定一个\(n(n\le50)\)个点,\(m(m\le1000 ...

  10. 【UOJ#388】【UNR#3】配对树(线段树,dsu on tree)

    [UOJ#388][UNR#3]配对树(线段树,dsu on tree) 题面 UOJ 题解 考虑一个固定区间怎么计算答案,把这些点搞下来建树,然后\(dp\),不难发现一个点如果子树内能够匹配的话就 ...

随机推荐

  1. Web自动化框架LazyUI使用手册(5)--模板工程:LazyUI-template详解

    概述: LazyUI-template: 提供Maven管理的,基于Spring+Testng的,包含常用浏览器driver的,方便连接各种数据库的java模板工程,并提供以百度搜索为例的第一个测试用 ...

  2. Erlang标准数据结构的选择

    Erlang标准数据结构的选择(金庆的专栏)gen_server with a dict vs mnesia table vs etshttp://stackoverflow.com/question ...

  3. Struts 1 之文件上传

    Struts 1 对Apache的commons-fileupload进行了再封装,把上传文件封装成FormFile对象 定义UploadForm: private FormFilefile; //上 ...

  4. Effective C++ ——设计与声明

    条款18:让接口更容易的被使用,不易误用 接口设计主要是给应用接口的人使用的,他们可能不是接口的设计者,这样作为接口的设计者就要对接口的定义更加易懂,让使用者不宜发生误用,例如对于一个时间类: cla ...

  5. JDK的安装以及配置

    JDK的安装以及配置 JDK(Java Development Kit),顾名思义,是 Java 语言的软件开发工具包(SDK). Android发开使用Java语言,所以装JDK是Android开发 ...

  6. windows平台下 c/c++进行http通信的教训

    由于需要使用c++开发一个桌面应用软件,需要用到http请求进行通讯,也是本人第一次进行网络相关的开发工作,遇到了不少坑. 由于是在windows下开发和使用的应用软件,自然而然想到了调用Window ...

  7. 查全率(召回率)、精度(准确率)和F值

    文献中的recall rate(查全率或召回率) and precision(精度)是很重要的概念.可惜很多中文网站讲的我都稀里糊涂,只好用google查了个英文的,草翻如下:召回率和精度定义: 从一 ...

  8. Android开发学习之路--传感器之初体验

    说到传感器,还是有很多的,有加速度啊,光照啊,磁传感器等等.当然android手机之所以称为智能手机,少不了这几款传感器的功劳了.下面就学习下了,这里主要学习光照,加速度和磁. 新建工程emSenso ...

  9. Nginx创建密码保护目录

    nginx 的根目录 为:/home/undoner/nginx-wwwnginx 访问地址 为:http://127.0.0.1本文实现对nginx根目录文件访问的权限控制 (1)nginx指定密码 ...

  10. Android简易实战教程--第五话《开发一键锁屏应用》

    转载请注明出处:http://blog.csdn.net/qq_32059827/article/details/51860900 点击打开链接 Device Administration 对于这个应 ...