当前位置:网站首页>[二叉数]对称二叉树
[二叉数]对称二叉树
2022-04-21 11:14:00 【Alson_Code】
一、题目描述
原文链接:101. 对称二叉树
具体描述:
给你一个二叉树的根节点 root , 检查它是否轴对称。
示例 1:

输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:

输入:root = [1,2,2,null,3,null,3]
输出:false
提示:
- 树中节点数目在范围 [1, 1000] 内
- -100 <= Node.val <= 100
进阶:你可以运用递归和迭代两种方法解决这个问题吗?
迭代两种方法解决这个问题吗?
二、思路分析
两种解法,其实大差不差,我们先来说说递归!
写递归需要三部曲:
- 第一:确定递归参数及返回值
- 第二:确定终止条件
- 第三:单层逻辑
第一:递归的返回值可以确定为boolean,参数的话其实比较的是左右子树!(注意哦不是左右孩子!)
第二:终止条件就是如果左右两个子树一个为空,一个不为空则返回false,两个都是空则返回true,或者两个val值不相同则返回false,如果相同则继续比较!
if (left == null && right != null) return false;
else if (left != null && right == null) return false;
else if (left == null && right == null) return true;
else if (left.val != right.val) return false;
第三:递归比较左子树的左孩子和右子树的右孩子(相当于比较外侧),如果前面两个相等,在递归比较左子树的右孩子和右子树的左孩子(相当于比较内侧),如果都想等则返回true!
第二种方法就是迭代法!利用栈和队列都可以,我们拿队列举例!
- 首先把左右子树放到队列中,弹出左右子树比较值,如果相等则把左右子树按照比较顺序添加到队列中!如果不相等则返回false!
- 循环至队列为空则结束循环!
三、AC代码
递归解法:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public boolean isSymmetric(TreeNode root) {
return compare(root.left, root.right);
}
public boolean compare(TreeNode left, TreeNode right){
// 终止条件
if (left == null && right != null) return false;
else if (left != null && right == null) return false;
else if (left == null && right == null) return true;
else if (left.val != right.val) return false;
// 单层循环体(如果左右子树相等,需要继续递归比较)
boolean outSideCompare = compare(left.left, right.right);
boolean insideCompare = compare(left.right, right.left);
return outSideCompare && insideCompare;
}
}
迭代解法:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */
class Solution {
public boolean isSymmetric(TreeNode root) {
Deque<TreeNode> deque = new LinkedList<>();
deque.offerFirst(root.left);
deque.offerFirst(root.right);
while (deque.isEmpty() == false){
TreeNode left = deque.pollFirst();
TreeNode right = deque.pollFirst();
if (left == null && right == null) continue;
if (left == null || right == null || left.val != right.val) return false;
deque.offerFirst(left.left);
deque.offerFirst(right.right);
deque.offerFirst(left.right);
deque.offerFirst(right.left);
}
return true;
}
}
四、总结
- 递归三步曲
感谢大家的阅读,我是Alson_Code,一个喜欢把简单问题复杂化,把复杂问题简单化的程序猿!
版权声明
本文为[Alson_Code]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_42415173/article/details/124308176
边栏推荐
- hyperf 执行sql语句,参数会有两个单引号
- Tami dog knowledge | what are the legal procedures for equity transfer?
- How does IOT platform realize business configuration center
- AC automata
- Suffix array special training
- [TIANTI race] l3-005 dustbin distribution (heap optimized version Dijkstra)
- 【面试普通人VS高手系列】b树和b+树的理解
- O2oa secondary development - use the open source platform to build a complete OA (3) - development enterprise reimbursement approval
- 塔米狗知识|股权转让合法程序有哪些?
- Brutally crack the latest JVM interview questions of meituan
猜你喜欢

How does IOT platform realize business configuration center

犀牛软件插件-rhino插件-visual studio-创建你的第一个插件

Tami dog knowledge | what are the legal procedures for equity transfer?

AcWing 1725. Team tic tac toe game (enumeration)
![[interview ordinary people vs Expert Series] understanding of B tree and B + tree](/img/17/a81da0a2e6b0c7cb218879f1a79861.jpg)
[interview ordinary people vs Expert Series] understanding of B tree and B + tree

Kubernetes 中数据包的生命周期 -- 第 1 部分

hyperf 执行sql语句,参数会有两个单引号

10000 yuan gift pool play "Lighthouse" prize essay attack

Rhino software plug-in rhino plug-in visual studio create your first plug-in

教你轻松解决CSRF跨站请求伪造攻击
随机推荐
Leetcode1615. 最大网络秩(medium,图论基础)
There was a problem during PIP install, unable to connect (PIP configure mirror source)
From station B and little red book, how to do a good job of community products?
5.4万Star全部归零,项目作者:十分后悔
从B站和小红书看,如何做好社区产品?
Rhino software plug-in rhino plug-in visual studio create your first plug-in
Still AC automata
安装nfs文件系统
Unix哲学与高并发
[非线性控制理论]1_Lyapunov直接方法
MATLAB GUI---学科选择(动画演示)
活动报名 | 如何基于开源项目 Tapdata PDK,快速完成数据源和目标的开发?
org. apache. flink. client. deployment. ClusterDeploymentException: Could not deploy Yarn job cluster.
make the inifile support unicode in delphi
package. json
AcWing 1749. Block billboard II (classified discussion + enumeration)
塔米狗资讯|国资委发言:支持上市公司采用企业并购融资手段拓展主业
IoT平台如何实现业务配置中心
IoT平台如何实现业务配置中心
Tami dog knowledge | what are the legal procedures for equity transfer?