java判断二叉树是不是对称_判断二叉树是否对称

27

public class Solution {

public static boolean isSymmetric(TreeNode root) {

return check(root, root);

}

public static boolean check(TreeNode x, TreeNode y) {

if(x == null && y == null) return true;

if((x == null && y != null) || (x != null && y == null)) return false;

return x.val == y.val && check(x.left, y.right) && check(x.right, y.left);

}

}

发表于 2016-11-01 21:29:49

回复(14)

9

//镜子对称性

bool isSymmetric(TreeNode *root) {

if(root==NULL)

return true;

return isEqual(root->left,root->right);

}

bool isEqual(TreeNode *root1,TreeNode *root2){

if(root1==NULL&&root2==NULL)

return true;

if(root1==NULL||root2==NULL)

return false;

if(root1->val != root2->val)

return false;

return isEqual(root1->left,root2->right)&&isEqual(root1->right,root2->left);

}

发表于 2016-10-19 20:34:01

回复(4)

8

import java.util.LinkedList;

import java.util.Queue;

/**

* 思路:(树的结点值都为个位数)

1

/ \

3 3

/ \ / \

4 5 5 4

/\ /\ /\ /\

9 7 62 26 7 9

...

假如树在一张纸上,对折纸后结点重合,重合处值相等,满足这样的树就是如题所说的“对称”。

以上图为例:

以 左3 作为根结点做前序遍历(根、左、右)依次遍历是

3、4、9、7、5、6、2 (这些元素都在1为根节点左子树)

以 右3 作为根节点做“对称前序遍历”(根、右、左)依次遍历是

3、4、9、7、5、6、2 (这些元素都在1为根节点右子树)

可以看出如果对称的话 前序遍历 和 “对称前序遍历”(根、右、左)的结果是一样的

(如果单单是先序遍历,然后对比该结点是不够的,因为先序遍历确定后,树的形状也可能是不同,故而还需要比较当前结点的左右结点情况)

从而也可以利用

中序遍历(左、根、右)和“对称中序遍历”(右、根、左)

后序遍历(左、右、根)和“对称后序遍历”(右、左、根)

来解这道题

* @author Rail

*

*/

public class IsSymmetric {

boolean isSame = true;

public boolean isSymmetric(TreeNode root) {

if(root == null)

return true;

reverse(root.left, root.right);

return isSame;

}

private void reverse(TreeNode left, TreeNode right) {

if(left == null && right == null)

return ;

if((left != null && right == null) || (left == null && right != null)

|| (left.val != right.val)){

isSame = false;

return ;

}else{

if(isSame){//避免在已经不相同的情况还继续遍历

reverse(left.left, right.right);

reverse(left.right, right.left);

}

}

}

/*非递归

* public boolean isSymmetric(TreeNode root) {

if(root == null)

return true;

boolean isMirror = true;

Queue queue = new LinkedList();

if(root.left == null || root.right == null){

return root.left == root.right;

}

queue.add(root.left);

queue.add(root.right);

while(!queue.isEmpty()){

int size = queue.size();

while(size > 0){

TreeNode left = queue.poll();

TreeNode right = queue.poll();

size -= 2;

if((left == null || right == null) && left != right)//不能删除里面的括号,&&优先级高于||

return false;

if(left == null || right == null)

continue;

if(left.val != right.val)

return false;

queue.add(left.left);

queue.add(right.right);

queue.add(left.right);

queue.add(right.left);

}

}

return isMirror;

}*/

}

编辑于 2017-10-18 01:54:36

回复(3)

5

C++ solution

递归调用,同时判断左子树的左节点与左子树的右节点,以及左子树的右节点与右子树的左节点。一旦这两个节点不相等,就返回false。 class Solution {

public:

bool isSymmetric(TreeNode *root) {

return helper(root, root);

}

bool helper(TreeNode *L, TreeNode *R) {

if (L == NULL and R == NULL) { return true; }

if (L and R and L->val == R->val) {

return helper(L->left, R->right) and helper(L->right, R->left);

}

return false;

}

};

编辑于 2017-10-27 17:29:33

回复(3)

3

判断两个二叉树是否相等好写

在这个基础上稍微改一改 将左右错开来即可 bool if_equal(TreeNode *a, TreeNode *b){

if(!a && !b) return 1;

if(!a || !b) return 0;

return (a->val == b->val) &&

if_equal(a->left, b->right) &&

if_equal(a->right, b->left);

}

bool isSymmetric(TreeNode *root) {

return if_equal(root, root);

}

发表于 2019-05-14 22:13:35

回复(0)

2

/**

* struct TreeNode {

*int val;

*struct TreeNode *left;

*struct TreeNode *right;

* };

*/

class Solution {

public:

/**

*

* @param root TreeNode类

* @return bool布尔型

*/

bool isSymmetric(TreeNode* root) {

//return isSameTree(root, reverseTree(root));//方法1,先翻转二叉树,再判断两颗二叉树是否相同

//方法2

if(!root) return true;

return judgeTree(root->left,root->right);

}

bool judgeTree(TreeNode* left,TreeNode* right){

if(!left&&!right) return true;

if(!left||!right||left->val!=right->val) return false;

return judgeTree(left->left, right->right)&&judgeTree(left->right, right->left);

}

//判断两棵树是否相同

bool isSameTree(TreeNode* a,TreeNode* b){

if(!a&&!b) return true;

if(!a||!b) return false;

if(a->val==b->val){

return isSameTree(a->left, b->left)&&isSameTree(a->right, b->right);

}

return false;

}

//树的左右子节点翻转

TreeNode* reverseTree(TreeNode* root){

if(root==nullptr) return nullptr;

TreeNode* res = new TreeNode(root->val);

res->left = reverseTree(root->right);

res->right = reverseTree(root->left);

return res;

}

};

发表于 2021-02-08 11:36:17

回复(0)

2

public class Solution {

// 层序遍历

/**

public boolean isSymmetric(TreeNode root) {

if (root == null) {

return true;

}

LinkedList queue = new LinkedList<>();

queue.offer(root.left);

queue.offer(root.right);

while (!queue.isEmpty()) {

int size = queue.size();

while (size > 0) {

TreeNode left = queue.poll();

TreeNode right = queue.poll();

size -= 2;

if (left == null && right == null) {

continue;

}

if ((left == null || right == null) || (left.val != right.val)) {

return false;

}

queue.offer(left.left);

queue.offer(right.right);

queue.offer(left.right);

queue.offer(right.left);

}

}

return true;

}

*/

// 递归

public boolean isSymmetric(TreeNode root) {

if (root == null) {

return true;

}

return helper(root.left, root.right);

}

private boolean helper(TreeNode left, TreeNode right) {

if (left == null && right == null) {

return true;

}

if (left == null || right == null || left.val != right.val) {

return false;

}

return helper(left.left, right.right) && helper(left.right, right.left);

}

}

编辑于 2019-01-25 14:25:33

回复(2)

2

腾讯实习面试原题: /**

* Definition for binary tree

* public class TreeNode {

* int val;

* TreeNode left;

* TreeNode right;

* TreeNode(int x) { val = x; }

* }

*/

public class Solution {

public boolean isSymmetric(TreeNode root) {

return helper(root, root);

}

// 递归: leftNode 的右子树和 rightNode 的左子树是否对称,leftNode 的左子树和 rightNode 的右子树是否对称

private boolean helper(TreeNode leftNode, TreeNode rightNode) {

if (leftNode == null && rightNode == null)

return true;

if ((leftNode == null && rightNode != null) || (leftNode != null && rightNode == null))

return false;

// leftNode 和 rightNode 都不为空,比较 val 是否相等

// 同时看 leftNode 的右子树和 rightNode 的左子树是否对称,leftNode 的左子树和 rightNode 的右子树是否对称

return (leftNode.val == rightNode.val) &&

helper(leftNode.right, rightNode.left) &&

helper(leftNode.left, rightNode.right);

}

}

发表于 2018-08-20 21:12:54

回复(0)

2

/recursive way

class Solution

{

public:

bool isSymmetric(TreeNode *root)

{

if(!root)

return true;

return compare(root->left,root->right);

}

private:

bool compare(TreeNode *node1,TreeNode *node2)

{

if(!node1 && !node2)

return true;

if(!node1 || !node2)

return false;

if(node1->val!=node2->val)

return false;

return compare(node1->left,node2->right) && compare(node1->right,node2->left);

}

};

iterative way

class Solution

{

public:

bool isSymmetric(TreeNode *root)

{

if(!root)

return true;

queue Q;

Q.push(root->left), Q.push(root->right);

while(!Q.empty())

{

int size=Q.size();

while(size)

{

TreeNode* left=Q.front();

Q.pop();

TreeNode* right=Q.front();

Q.pop();

size-=2;

if(!left && !right)

continue;

if(!left || !right)

return false;

if(left->val!=right->val)

return false;

Q.push(left->left), Q.push(right->right);

Q.push(left->right), Q.push(right->left);

}

}

return true;

}

};

发表于 2017-10-08 17:01:32

回复(0)

2

bool isSymmetric(TreeNode *root) {

if(root==NULL)

return 1;

if(root->left==NULL&&root->right==NULL)

return 1;

if(root->left!=NULL&&root->right!=NULL)

return sym(root->left, root->right);

return 0;

}

bool sym(TreeNode *l, TreeNode *r)

{

if(l==NULL&&r==NULL)

return 1;

if((l==NULL&&r!=NULL)||(l!=NULL&&r==NULL))

return 0;

if(l->val!=r->val)

return 0;

return sym(l->left, r->right)&&sym(l->right, r->left);

}

发表于 2017-09-18 00:06:56

回复(0)

2

//先将左子树或者右子树翻转,然后判断左子树和右子树是否相等

public boolean isSymmetric(TreeNode root) {

if (root == null) return true;

TreeNode right = reverse(root.right);

TreeNode left = root.left;

return TreeEqual(left,right);

}

//将树翻转

public TreeNode reverse(TreeNode root){

if(root == null) return null;

if(root.left != null) reverse(root.left);

if(root.right != null) reverse(root.right);

TreeNode node = root.left;

root.left = root.right;

root.right = node;

return root;

}

//判断两棵树是否相等

public boolean TreeEqual(TreeNode root1,TreeNode root2){

if(root1 == null && root2 == null) return true;

if((root1 == null && root2 != null) ||(root1 != null && root2 == null)) return false;

return TreeEqual(root1.left,root2.left) && TreeEqual(root1.right,root2.right) && root1.val == root2.val;

}

发表于 2017-08-22 10:04:48

回复(0)

2

class Solution {

public:

bool isSymmetric(TreeNode *root) {

if(root == NULL)

return true;

return isEqual(root->left,root->right);

}

bool isEqual(TreeNode *L,TreeNode *R)

{

if(L==NULL && R==NULL)

return true;

else if(L==NULL || R==NULL)

return false;

else if(L->val != R->val)

return false;

else

return isEqual(L->left,R->right) && isEqual(L->right,R->left);

}

};

发表于 2017-08-14 01:36:30

回复(0)

1

C++:

/*

* public class TreeNode {

*   int val = 0;

*   TreeNode left = null;

*   TreeNode right = null;

* }

*/

class Solution {

public:

bool isSymmetric(TreeNode *root) {

return check(root, root);

}

bool check(TreeNode *x, TreeNode *y) {

if (x == nullptr && y == nullptr) return true;

if ((x == nullptr && y != nullptr) || (x != nullptr && y == nullptr)) return false;

return x->val == y->val && check(x->left, y->right) && check(x->right, y->left);

}

}; Java:

/*

* public class TreeNode {

*   int val = 0;

*   TreeNode left = null;

*   TreeNode right = null;

* }

*/

public class Solution {

public static boolean isSymmetric(TreeNode root) {

return check(root, root);

}

public static boolean check(TreeNode x, TreeNode y) {

if (x == null && y == null) return true;

if ((x == null && y != null) || (x != null && y == null)) return false;

return x.val == y.val && check(x.left, y.right) && check(x.right, y.left);

}

}

发表于 2021-02-04 09:53:57

回复(0)

1

/**

* struct TreeNode {

*int val;

*struct TreeNode *left;

*struct TreeNode *right;

* };

*/

class Solution {

public:

/**

*

* @param root TreeNode类

* @return bool布尔型

*/

bool is_symmetrical(TreeNode *pRoot1, TreeNode *pRoot2){//判断2棵树是否对称

if (pRoot1 == NULL && pRoot2 == NULL) return true;

if (pRoot1 != NULL && pRoot2 != NULL) {

if (pRoot1->val == pRoot2->val) {

if (is_symmetrical(pRoot1->left, pRoot2->right) && is_symmetrical(pRoot1->right, pRoot2->left))

return true;

}

}

return false;

}

bool isSymmetric(TreeNode* root) {

// write code here

if (root == NULL || (root->left == NULL && root->right == NULL)) return true;

if ((root->left != NULL && root->right == NULL) ||(root->right != NULL && root->left == NULL)) return false;

return is_symmetrical(root->left, root->right);

}

};

发表于 2020-11-28 16:57:39

回复(0)

1

import java.util.*;

/*

* public class TreeNode {

*   int val = 0;

*   TreeNode left = null;

*   TreeNode right = null;

* }

*/

public class Solution {

/**

*

* @param root TreeNode类

* @return bool布尔型

*/

public boolean isSymmetric (TreeNode root) {

if(root == null) {

return true;

}

return isSymmetric2(root.left, root.right);

}

public boolean isSymmetric2 (TreeNode root1, TreeNode root2) {

if(root1 == null && root2 == null){

// 两个节点相等,并且均为 null

return true;

}else if(root1 == null || root2 == null || root1.val != root2.val) {

return false;

}

return isSymmetric2(root1.left, root2.right) && isSymmetric2(root1.right, root2.left);

}

}

发表于 2020-11-28 16:07:10

回复(0)

1

可以考虑同时对root的两个根节点进行相反顺序的遍历。

比如左子节点使用根->左->右的顺序,则右子节点使用根->右->左的顺序。

public  boolean isSymmetric(TreeNode root) {

// write code here

if (Objects.isNull(root)) {

return true;

}

List leftList = new ArrayList<>();

List rightList = new ArrayList<>();

leftList.add(root.left);

rightList.add(root.right);

while (!leftList.isEmpty() && !rightList.isEmpty()) {

TreeNode l = leftList.get(0);

TreeNode r = rightList.get(0);

if (l == r) { //假如同时为null

leftList.remove(0);

rightList.remove(0);

continue;

} else if (Objects.isNull(l) || Objects.isNull(r)) {

return false;

} else if (l.val != r.val) {

return false;

}

leftList.remove(0);

rightList.remove(0);

leftList.add(l.right);

leftList.add(l.left);

rightList.add(r.left);

rightList.add(r.right);

}

try { //处理一下边界条件

if (leftList.get(0) != rightList.get(0)) {

return false;

}

} catch (IndexOutOfBoundsException e) {

return true;

}

return true;

}

编辑于 2020-11-28 00:17:10

回复(0)

1

public class Solution {

public boolean isSymmetric (TreeNode root) {

if(root == null) return true;

return isSym(root.left, root.right);

}

public boolean isSym(TreeNode l,TreeNode r)

{

if(l == null || r == null) return l == r;

return l.val == r.val && isSym(l.left,r.right) && isSym(l.right,r.left);

}

}

发表于 2020-09-15 21:45:20

回复(0)

1

根左右==根右左

/**

* struct TreeNode {

*int val;

*struct TreeNode *left;

*struct TreeNode *right;

* };

*/

class Solution {

public:

/**

*

* @param root TreeNode类

* @return bool布尔型

*/

string line_tree_l(TreeNode *head){

if(head == NULL){

return "#";

}

return to_string(head->val) + line_tree_l(head->left) + line_tree_l(head->right);

}

string line_tree_r(TreeNode *head){

if(head == NULL){

return "#";

}

return to_string(head->val) + line_tree_r(head->right) + line_tree_r(head->left);

}

bool isSymmetric(TreeNode* root) {

// write code here

string res_l = line_tree_l(root);

string res_r = line_tree_r(root);

return res_l == res_r;

}

};

发表于 2020-08-27 16:47:34

回复(0)

1

class Solution {

public:

bool isSymmetric(TreeNode *root) {

return isSymmetric2(root, root);

}

// 递归方案

bool isSymmetric(TreeNode *root1, TreeNode *root2) {

if (root1 == nullptr || root2 == nullptr) return root1 == root2;

bool sym = isSymmetric(root1 -> left, root2 -> right) & isSymmetric(root1 -> right, root2 -> left);

return sym && root1 -> val == root2 -> val;

}

// 迭代方案,层序遍历

bool isSymmetric2(TreeNode *root1, TreeNode *root2) {

queue q1, q2;

q1.push(root1);

q2.push(root2);

while (q1.size() > 0 && q2.size() > 0) {

TreeNode* p1 = q1.front();

q1.pop();

TreeNode* p2 = q2.front();

q2.pop();

if ((p1 == nullptr || p2 == nullptr) && p1 != p2) return false;

if (p1 == nullptr) continue;

if (p1 -> val != p2 -> val) return false;

q1.push(p1->left);

q1.push(p1->right);

q2.push(p2->right);

q2.push(p2->left);

}

return q1.size() == q2.size();

}

};

发表于 2020-03-31 18:52:54

回复(0)

1

public class Solution {

public boolean isSymmetric(TreeNode root) {

return process(root, root);

}

public boolean process(TreeNode node1, TreeNode node2) {

if (node1 == null && node2 == null) {

return true;

}

if (node1 == null || node2 == null) {

return false;

}

if (node1.val != node2.val) {

return false;

}

return process(node1.left, node2.right) && process(node1.right, node2.left);

}

}

发表于 2019-11-06 16:03:31

回复(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值