博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
38. Same Tree && Symmetric Tree
阅读量:5333 次
发布时间:2019-06-15

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

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 binary tree * 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 == NULL && q) || (p && q == NULL)) return false;        if(p == NULL && q == NULL) return true;        if(p->val != q->val) return false;        return isSameTree(p->left, q->left) && isSameTree(p->right, q->right);    }};

 

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.

思想: 构造其镜像树。

1. 递归。用 Same Tree方法判断即可

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */TreeNode* getMirror(TreeNode *root) {    if(root == NULL) return NULL;    TreeNode *p = new TreeNode(root->val);    p->left = getMirror(root->right);    p->right = getMirror(root->left);    return p;}bool isSymmetricCore(TreeNode *root, TreeNode *root2) {    if((!root && root2) || (root && !root2)) return false;    if(!root && !root2) return true;    if(root->val != root2->val) return false;     return isSymmetricCore(root->left, root2->left) && isSymmetricCore(root->right, root2->right);}class Solution {public:    bool isSymmetric(TreeNode *root) {        TreeNode *mirrorTree = getMirror(root);        return isSymmetricCore(root, mirrorTree);    }};

 2. 迭代。两棵树相同方法遍历即可。

/** * Definition for binary tree * struct TreeNode { *     int val; *     TreeNode *left; *     TreeNode *right; *     TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */TreeNode* getMirror(TreeNode *root) {    if(root == NULL) return NULL;    TreeNode *p = new TreeNode(root->val);    p->left = getMirror(root->right);    p->right = getMirror(root->left);    return p;}bool pushChildNode(TreeNode *p1, TreeNode *p2, queue
& qu, queue
& qu2) { if(p1->left && p2->left) { qu.push(p1->left); qu2.push(p2->left); } else if(p1->left || p2->left) return false; if(p1->right && p2->right) { qu.push(p1->right); qu2.push(p2->right);} else if(p1->right || p2->right) return false; return true;}class Solution {public: bool isSymmetric(TreeNode *root) { if(root == NULL) return true; TreeNode *mirrorTree = getMirror(root); queue
que1; queue
que2; que1.push(root); que2.push(mirrorTree); while(!que1.empty() && !que2.empty()) { TreeNode *p1 = que1.front(), *p2 = que2.front(); que1.pop(); que2.pop(); if(p1->val != p2->val) return false; if(!pushChildNode(p1, p2, que1, que2)) return false; } if(!que1.empty() || !que2.empty()) return false; return true; }};

 

转载于:https://www.cnblogs.com/liyangguang1988/p/3940137.html

你可能感兴趣的文章
ava集合框架
查看>>
把sublime text2配置的更好用,用到一点写一点
查看>>
构建之法阅读笔记02
查看>>
核心动画 (基本使用) UIView与核心动画对比?
查看>>
asp.net无法触发asp控件的后台方法
查看>>
(OK) 国内常用NTP服务器地址及IP
查看>>
PLSQL--存储过程
查看>>
第十五周翻译
查看>>
数据库sqlite的简单应用
查看>>
AIX中PV,VG,LV及FS常用相关命令
查看>>
Elasticsearch 之(23)IK中文分词器的安装和使用
查看>>
【树的遍历转换】American Heritage美国血统
查看>>
【网络流之最大流专题】解题报告
查看>>
CentOs环境下给PHP7.0安装fileinfo扩展
查看>>
UI_KVC赋值
查看>>
【刷题小记67】三角形面积
查看>>
Task
查看>>
【AS3代码】精确到点的不规则图形碰撞检测
查看>>
理解数据集
查看>>
CCDictionary
查看>>