对称树

Problem statement:

问题陈述:

Given a binary Tree, check whether the tree is symmetric or not.

给定二叉树 , 检查树是否对称

Input Example:

输入示例:

For example
1
/ \
2   2
/ \ / \
3  4 4  3
The above tree is symmetric
1
/ \
2   2
\   \
4    4
The above tree is not

Solution

对称树 (Symmetric tree)

A binary tree is said to be symmetric, if a vertical axis through the root cuts the tree in two mirror subtrees. That means the right child & left child of root are two mirror trees.

如果通过根的垂直轴在两个镜像子树中切割树,则二叉树被称为对称树。 这意味着根的右子和左子是两棵镜像树。

Algorithm:

算法:

Earlier, we have discussed how two check mirror trees? Kindly refer there for more detailed analysis.

前面我们讨论了如何检查两个镜像树 ? 请参考那里进行更详细的分析。

In this case we are to check whether the tree is symmetric or not, which can be done via checking whether root's children are mirror trees or not?

在这种情况下,我们要检查树是否对称 ,这可以通过检查根的子节点是否为镜像树来完成?

FUNCTION isSymmetric(Tree Node* root) 
1.  Check for base cases
IF root==NULL //if root is NULL
Return true;
IF root->left==NULL &&root->right==NULL //if both child is NULL
Return true;
IF root->left==NULL || root->right==NULL //if only one child is NULL
Return false;
2.  check whether left & right subtrees are mirror of each other or not 
Return check(root->left,root->right);
END FUNCTION

FUNCTION Check(Tree Node* root1, Tree Node* root2)
a.  Check base cases
If root1==NULL && root2==NULL
Then it's mirror tree, return true;
If root1==NULL || root2==NULL
Then it's not a mirror tree, return false
Because one root is NULL whether another is not. 
(Both can't be NULL here, since already checked before) 
If root1->data != root2->data
Then it's not a mirror tree, return false.
Because roots are different & thus can't be mirror image of other
b.  Recursively check for sub-trees
Return (Check (root1->left, root2->right) &&Check(root1->right,root2->left));
END FUNCTION

Example with explanation

带说明的例子

For the example:
1
/ \
2   2
/ \ / \
3  4 4  3
At the main function:
It calls isSymmetric(1)
-----------------------------------------------------------
isSymmetric(1):
Base cases are not met
It calls check(1->left, 1->right)
Root1=2(1->left) and Root2=2(1->right)
It calls check (2, 2)
-----------------------------------------------------------
check (2, 2):
2->left =3 and 2->right=4 in case of tree1
2->left =4 and 2->right=3 in case of tree2
No base cases are satisfied thus it returns,
(check ( 3, 3) && check ( 4, 4))
Call to check ( 3, 3) and check ( 4, 4)
-----------------------------------------------------------
check (3, 3):(call at check (2, 2))
3->left =NULL and 3->right=NULL in case of tree1
3->left =NULL and 3->right=NULL in case of tree2
No base cases are satisfied thus it returns,
(check (NULL, NULL) &&check (NULL, NULL))
Call to check (NULL, NULL) and check (NULL, NULL))
-----------------------------------------------------------
check (4, 4): (call at check (2, 2))
4->left =NULL and 4->right=NULL in case of tree1
4->left =NULL and 4->right=NULL in case of tree2
No base cases are satisfied thus it returns,
(check (NULL, NULL) &&check (NULL, NULL))
Call to check (NULL, NULL) &&check (NULL, NULL)
-----------------------------------------------------------
check (NULL, NULL): (call at check (3, 3))
Base case matches and returns 1. 
-----------------------------------------------------------
check (NULL, NULL): (call at check (3, 3))
Base case matches and returns 1. 
-----------------------------------------------------------
check (NULL, NULL): (call at check (4, 4))
Base case matches and returns 1. 
-----------------------------------------------------------
check (NULL, NULL): (call at check (4, 4))
Base case matches and returns 1. 
-----------------------------------------------------------
Thus at isSymmetric(1),
check (2, 2) returns:
=   check ( 3, 3) &&check ( 4, 4)
=   ((check (NULL, NULL)&&check (NULL, NULL)) 
&&((check (NULL, NULL)&&check (NULL, NULL))
=   ((1 && 1)) && (1 && 1))
=   1
Thus at main it returns 1
So it's a symmetric tree

C++ implementation

C ++实现

#include<bits/stdc++.h>
using namespace std;
// TreeNode node type
class TreeNode{
public:             
int val;           //value
TreeNode *left;    //pointer to left child
TreeNode *right;   //pointer to right child
};
// creating new node
TreeNode* newnode(int data)  
{ 
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)); 
node->val = data; 
node->left = NULL; 
node->right = NULL; 
return(node); 
}
// function to check mirror trees
bool check(TreeNode* root1,TreeNode* root2){
// base cases
//if both root is NULL, then it's mirror tree
if(!root1 && !root2)
return true;
//if one is NULL & other is not
//then it's not mirror tree
if(!root1 || !root2)
return false;
//if root data are different it's not mirror tree
if(root1->val!=root2->val)
return false;
//check for subtrees
return (check(root1->left,root2->right)&&check(root1->right,root2->left));
}
bool isSymmetric(TreeNode* root) {
//base cases
if(!root)
return true;
if(!root->left && !root->right)
return true;
if(!root->left || !root->right)
return false;
//check whether left & right subtrees 
//are mirror of each other or not 
return check(root->left,root->right);
}
int main(){
cout<<"tree is built as per example\n";
TreeNode *root=newnode(1); 
root->left= newnode(2); 
root->right= newnode(2); 
root->right->right=newnode(3);
root->right->left=newnode(4);
root->left->left=newnode(3); 
root->left->right=newnode(4);
if(isSymmetric(root))
cout<<"It's a symmetric tree\n";
else
cout<<"It's not a symmetric tree\n";
return 0;
}

Output

输出量

tree is built as per example
It's a symmetric tree 

翻译自: https://www.includehelp.com/icp/symmetric-tree.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543737.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

unity, undo

如果在操作一个Object之前调用Undo.RecordObject(Object)&#xff0c;且操作确实造成Object某些属性的改变&#xff0c;则会产生一个undo记录。 如果我们的架构不是直接操作Object&#xff0c;而是操作一个ui&#xff0c;并在某些时机通过ui.save(Object)将数据回写到Object&am…

ajax应用_AJAX的应用

ajax应用AJAX has several benefits that can be utilized inside a web application. In this article, well explore some advantages of AJAX and see some of its applications. AJAX具有可在Web应用程序内部使用的多个优点。 在本文中&#xff0c;我们将探讨AJAX的一些优势…

Linux下DRBD配置

一、什么是DRBD1、简介 Distributed Replicated Block Device(DRBD)是一个用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。数据镜像&#xff1a;实时、透明、同步&#xff08;所有服务器都成功后返回&#xff09;、异步&#xff08;本地服务器成功后返回…

键盘特殊_特殊键盘

键盘特殊Problem statement: 问题陈述&#xff1a; Imagine you have a special keyboard with four types of keys: 想象一下&#xff0c;您有一个特殊的键盘&#xff0c;其中包含四种类型的键&#xff1a; Key 1: Prints I on screen 按键1&#xff1a;在屏幕上打印“ I”…

【C++入门】简单的日期类操作

//--------------------------------------------------------------------------/***名称&#xff1a;日期的简单操作******类函数&#xff1a;构造函数&#xff0c;拷贝构造函数&#xff0c;析构函数&#xff0c;操作符重载函数****日期类操作函数&#xff1a; 1&#xff1a;…

Scala山脉

Scala Range Scala山脉 A Range is a bounded series with a uniform interval with an upper and lower limit. The range literal is a numerical sequence of number ranging with a certain limit. 范围是一个有上限且下限均匀的有界序列。 范围文字是具有一定限制的范围…

黑客经验谈:跳板攻击入侵技术实例解析

网络入侵&#xff0c;安全第一,一个高明的入侵者&#xff0c;不会冒然实行动. 他们在入侵时前会做足功课&#xff0c;入侵时会通过各种技术手段保护自己&#xff0c;以防被对方发现&#xff0c;引火烧身. 其中&#xff0c;跳板技术是攻击者通常采用的技术. 下面笔者结合实例&am…

dom属性和html属性_HTML属性

dom属性和html属性Attributes are used to provide additional information of a tag such as it’s alignments, color, size of the text and other. The attributes are given with the tag that is between the angular brackets after the tag name. The attributes have …

科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系

UTF-8&#xff1a;Unicode TransformationFormat-8bit&#xff0c;允许含BOM&#xff0c;但通常不含BOM。是用以解决国际上字符的一种多字节编码&#xff0c;它对英文使用8位&#xff08;即一个字节&#xff09;&#xff0c;中文使用24为&#xff08;三个字节&#xff09;来编码…

vue3实现本地开发使用的px转换成vw,px转换成rem方法整理

前言&#xff1a; 项目中如果想本地开发使用px&#xff0c;但是界面上线以后界面是自适应的效果,可以有多种方式来实现效果。 一、px转成vw 1、安装&#xff0c;安装成功后&#xff0c;node_modules 会新增这两个插件包 npm i postcss-px-to-viewport-8-plugin 2、新增 post…

airplay2协议是什么_什么是AirPlay?

airplay2协议是什么AirPlay (AirPlay) AirPlay is released by Apple in the year 2004. It allows the easy exchange of audios without the use of any wired technique between the two devices. It was previously termed as AirTunes and later got its name changed to …

微信支付开发(5) 订单查询

本文介绍微信支付中订单查询功能的实现。 作者&#xff1a;方倍工作室 地址&#xff1a;http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一、订单查询 因为某一方技术的原因&#xff0c;可能导致商户在预期时间内都收不到最终支付通知&#xff0c;此时商户可以通过该…

ruby 执行函数_Ruby at()函数

ruby 执行函数Ruby中的at()函数 (at() function in Ruby) If you are working with arrays in Ruby, sometimes you may need to find the element at a particular index. For meeting the purpose, we have got at() function in Ruby which is already defined in Rubys lib…

python饼形图_Python | 饼形图

python饼形图A pie plot or a pie chart is a circular statistical graphic technique, in which a circle is divided into slices with respect to numerical proportion. In a pie chart, the arc length, central angle, and area of each slice, is proportional to the …

Linux巡检

# uname -a # 查看内核/操作系统/CPU信息 # head -n 1 /etc/issue # 查看操作系统版本 # cat /proc/cpuinfo # 查看CPU信息 # hostname # 查看计算机名 # lspci -tv # 列出所有PCI设备 # lsusb -tv # 列出所有USB设备 # lsmod # 列出加载的内核模块 # env # 查看环境变量 # fre…

appweb ejs_EJS部分

appweb ejsHi! Welcome. Today, we are going to look at EJS partials. EJS Partials help us avoid repetition of the same code on several web pages. 嗨&#xff01; 欢迎。 今天&#xff0c;我们将看EJS局​​部函数 。 EJS Partials帮助我们避免在多个网页上重复相同的…

Struts2配置

1. 设定server a) window– preferences – myeclipse – servers – tomcat – 6.x b) 选择tomcat homedirectory c) 选择enable d) finish 2. 设定jdk环境 a) window– preferences – java – installed jres b) 如果没有对应的JDK…

ruby继承_Ruby继承

ruby继承Ruby中的继承 (Inheritance in Ruby) Inheritance is a feature of Object Oriented languages in which new classes are derived from existing classes and resulting in the formation of a hierarchy of classes. The derived class is often called as child cla…

Spring与Hibernate整合中,使用OpenSessionInViewFilter后出现sessionFactory未注入问题

近期在知乎看到一句话&#xff0c;保持学习的有一种是你看到了很多其它的牛人&#xff0c;不甘心&#xff0c;真的不甘心。Spring和hibernate整合的时候&#xff0c;jsp页面做展现&#xff0c;发现展现属性出现&#xff1a; org.apache.jasper.JasperException: could not init…

sql判断数据库类型数据_SQL数据类型

sql判断数据库类型数据SQL | 资料类型 (SQL | Data Types) Just like other programming languages, facilities of defining data of various types are available in SQL also. SQL supports the following data types for the specification of various data-items or field…