对称树
Problem statement:
問(wèn)題陳述:
Given a binary Tree, check whether the tree is symmetric or not.
給定二叉樹(shù) , 檢查樹(shù)是否對(duì)稱(chēng) 。
Input Example:
輸入示例:
For example1/ \2 2/ \ / \ 3 4 4 3The above tree is symmetric1/ \2 2\ \4 4 The above tree is notSolution
解
對(duì)稱(chēng)樹(shù) (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.
如果通過(guò)根的垂直軸在兩個(gè)鏡像子樹(shù)中切割樹(shù),則二叉樹(shù)被稱(chēng)為對(duì)稱(chēng)樹(shù)。 這意味著根的右子和左子是兩棵鏡像樹(shù)。
Algorithm:
算法:
Earlier, we have discussed how two check mirror trees? Kindly refer there for more detailed analysis.
前面我們討論了如何檢查兩個(gè)鏡像樹(shù) ? 請(qǐng)參考那里進(jìn)行更詳細(xì)的分析。
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?
在這種情況下,我們要檢查樹(shù)是否對(duì)稱(chēng) ,這可以通過(guò)檢查根的子節(jié)點(diǎn)是否為鏡像樹(shù)來(lái)完成?
FUNCTION isSymmetric(Tree Node* root) 1. Check for base casesIF root==NULL //if root is NULLReturn true;IF root->left==NULL &&root->right==NULL //if both child is NULLReturn true;IF root->left==NULL || root->right==NULL //if only one child is NULLReturn 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 casesIf root1==NULL && root2==NULLThen it's mirror tree, return true;If root1==NULL || root2==NULLThen it's not a mirror tree, return falseBecause one root is NULL whether another is not. (Both can't be NULL here, since already checked before) If root1->data != root2->dataThen it's not a mirror tree, return false.Because roots are different & thus can't be mirror image of otherb. Recursively check for sub-treesReturn (Check (root1->left, root2->right) &&Check(root1->right,root2->left)); END FUNCTIONExample with explanation
帶說(shuō)明的例子
For the example:1/ \2 2/ \ / \ 3 4 4 3At 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)) = 1Thus at main it returns 1 So it's a symmetric tree .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}C++ implementation
C ++實(shí)現(xiàn)
#include<bits/stdc++.h> using namespace std;// TreeNode node type class TreeNode{public: int val; //valueTreeNode *left; //pointer to left childTreeNode *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 treeif(!root1 && !root2)return true;//if one is NULL & other is not//then it's not mirror treeif(!root1 || !root2)return false;//if root data are different it's not mirror treeif(root1->val!=root2->val)return false;//check for subtreesreturn (check(root1->left,root2->right)&&check(root1->right,root2->left)); }bool isSymmetric(TreeNode* root) {//base casesif(!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";elsecout<<"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
總結(jié)
- 上一篇: C ++中带有示例的llabs()函数
- 下一篇: 人类一败涂地做图教程_绘画步骤_人类一败