/ * タイトル: 与えられた対称バイナリツリーかどうかを判断します。 * / / * アイデア: 1、再帰的な方法。 図2に示すように、基本的な条件:2つのツリーは、真の空されている、ヌル・ツリーは、ツリーは、nullでない偽であるツリーのルートの2つの異なる値、falseに。 図3に示すように、さらに右サブツリー1及びツリー2子が対称左かどうかツリーの左サブツリー及び右サブツリー2が対称であるツリーを決定し、それが決定されます。 * / の#include <iostreamの> する#include <string.hの> する#include <アルゴリズム> の#include <cmath> する#include <stdio.hの> 使用して名前空間STD; 構造体BinaryTreeNode { int型のVal; BinaryTreeNode *は左; BinaryTreeNode *右; BinaryTreeNode (INT X): ヴァル(X)、(NULL)左、右(NULL){ } }; BOOL isSymmetrical(BinaryTreeNode * pRoot1、BinaryTreeNode pRoot2 *){ IF(pRoot1 == nullptr && pRoot2 == nullptr){ trueを返します。 } BinaryTreeNode *ノード1 =新しいBinaryTreeNode(8)。 BinaryTreeNode *ノード2 =新しいBinaryTreeNode(6); (pRoot1 == nullptr || pRoot2 == nullptr)戻り場合はfalse; 偽のブールフラグ=。 IF(pRoot1->ヴァル== pRoot2->ヴァル){ フラグ= isSymmetrical(pRoot1->左、pRoot2->右)&& isSymmetrical(pRoot1->右、pRoot2->左)。 } フラグを返します。 } BOOL isSymmetrical(BinaryTreeNode * PROOT){ IF(PROOT == nullptr)リターン偽。 リターンisSymmetrical(pRoot->左、pRoot->右)。 } int型のmain(){ BinaryTreeNode *ノード4 =新しいBinaryTreeNode(5)。 BinaryTreeNode * NODE5 =新しいBinaryTreeNode(7); BinaryTreeNode * node6 =新しいBinaryTreeNode(7); BinaryTreeNode * node7 =新しいBinaryTreeNode(5); node1->左=ノード2。 node1->右=ノード3。 node2->左=ノード4。 node2->右= NODE5。 node3->左= node6。 node3->右= node7。 COUT << isSymmetrical(ノード1)<< ENDL。 }