leetcode 101 Symmetric Tree

Given a binary tree, check whether it is a mirror of itself (ie:即, symmetric around its center:围绕其中心对称).


For example, this binary tree(二叉树) is symmetric(对称的):


    1
   / \
  2   2
 / \ / \
3  4 4  3
But the following is not:
    1
   / \
  2   2
   \   \
   3    3


Note:
Bonus points if you could solve it both recursively(递归) and iteratively(迭代).


confused what "{1,#,2,3}" means? > read more on how binary tree is serialized(序列) on OJ.(OJ是Online Judge系统的简称,用来在线检测程序源代码的正确性)


Hide Tags Tree Depth-first Search(树的深度优先搜索)


解题思路:根节点进行左右子节点判断,子节点对其同级子节点进行子节点的左右判断,使用递归与迭代的思想即可实现。

代码如下:

	 public class TreeNode
	 {
		 int val;
		 TreeNode left;
		 TreeNode right;
		 TreeNode(int x) 
		 { 
			 val = x;
		 }
	 }
	 
	 public  boolean isSymmetric(TreeNode root)
	 {
		 if(root==null)
		 {
			 return false;
		 }	 	 
		 else
		 {
			 boolean output=equalNode(root.left,root.right);
			 return output;
		 }	 
	 }
	 
	 public  boolean equalNode(TreeNode p,TreeNode q)
	 {
		 //如果需要判定的两节点均为空,返回true
		 if(p==null&&q==null)
		 {
			return true;
		 }
		 //如果需要判定的两节点有一个为空,另一个非空,返回false
		 if(p==null&&q!=null)
		 {
			 return false;
		 }
		 if(p!=null&&q==null)
		 {
			 return false;
		 }
		 //如果需要判定的两节点值相等,则继续往下判定,此处采用递归与迭代思想
		 if(p.val==q.val)
		 {
			 return equalNode(p.left,q.right)&&equalNode(p.right,q.left);
		 }
		 else
		 {
			 return false;
		 }
		 
	 }



### LeetCode 101 对称二叉树 C语言实现 #### 递归方法 为了判断一棵二叉树是否为对称结构,可以采用递归的方法来比较两棵子树。具体来说,要验证整棵树是否对称,则需确认其左子树与右子树互为镜像。 对于任意节点而言,只有当该节点的左右孩子均为空或两者皆不为空且值相等时才满足条件;接着再分别对比当前节点左侧孩子的左子树同右侧孩子的右子树以及左侧孩子的右子树同右侧孩子的左子树之间的关系即可[^4]。 下面是具体的C语言代码: ```c /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ bool isMirror(struct TreeNode* t1, struct TreeNode* t2){ if (t1 == NULL && t2 == NULL) return true; // 如果两个都是NULL则返回true if (t1 == NULL || t2 == NULL) return false; // 只有一个是NULL则不对称 // 值相同的情况下继续检查各自的左右分支是否也构成镜子映射的关系 return (t1->val == t2->val) && isMirror(t1->right, t2->left) && isMirror(t1->left, t2->right); } bool isSymmetric(struct TreeNode* root){ return isMirror(root, root); // 调用辅助函数isMirror来进行实际运算 } ``` 此段程序通过定义了一个名为`isMirror()`的帮助函数用于检测给定的一对节点`t1`和`t2`所代表的子树之间是否存在镜面对应关系,并最终利用它完成整个过程中的核心逻辑处理工作[^5]。 #### 迭代方法 除了上述提到的基于栈帧调用来解决问题的方式外,还可以借助队列这种数据结构以迭代的形式达成同样的目的——即每次取出一对待考察的对象并将其对应的四个方向上的邻接点依次入队等待后续进一步检验直至遍历完毕为止[^1]。 以下是使用广度优先搜索(BFS)策略下的另一种可能解决方案: ```c #include <stdbool.h> #include <stdlib.h> typedef struct QueueNode{ struct TreeNode *data; struct QueueNode *next; }QueueNode; // 初始化队列操作... void initQueue(QueueNode **front, QueueNode **rear); // 入队操作... void enqueue(QueueNode **front, QueueNode **rear, struct TreeNode *item); // 出队操作... struct TreeNode* dequeue(QueueNode **front, QueueNode **rear); bool check(struct TreeNode *u, struct TreeNode *v){ QueueNode *q_front = NULL,* q_rear = NULL; initQueue(&q_front,&q_rear); enqueue(&q_front,&q_rear,u); enqueue(&q_front,&q_rear,v); while(q_front != NULL){ u = dequeue(&q_front,&q_rear); v = dequeue(&q_front,&q_rear); if(!u && !v) continue; if((!u||!v)||(u->val!=v->val)) return false; enqueue(&q_front,&q_rear,u->left); enqueue(&q_front,&q_rear,v->right); enqueue(&q_front,&q_rear,u->right); enqueue(&q_front,&q_rear,v->left); } return true; } bool isSymmetric(struct TreeNode* root){ return check(root,root); } ``` 这段代码实现了非递归版的算法流程,在这里引入了额外的数据容器(如链表形式表示的队列),以便于按照层次顺序逐层访问各个顶点及其关联边的信息从而达到预期效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值