日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

对称树

發(fā)布時(shí)間:2025/3/11 编程问答 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对称树 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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 not

Solution

對(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 FUNCTION

Example 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é)

以上是生活随笔為你收集整理的对称树的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。