对称的二叉树
来源:互联网 发布:vb程序设计第二版答案 编辑:程序博客网 时间:2024/05/16 19:02
1、链接:对称的二叉树
来源:牛客网
请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。
2、思路:采用递归
3、代码:
boolean isSymmetrical(TreeNode pRoot) { boolean res = true; if (pRoot == null) return true; res = isSame(pRoot.left, pRoot.right); return res; } private boolean isSame(TreeNode left_root, TreeNode right_root) { boolean isOutsideSame = false; boolean isInsideSame = false; if ((left_root.left != null && right_root.right == null)|| (left_root.left == null && right_root.right != null)|| (left_root.right != null && right_root.left == null)|| (left_root.right == null && right_root.left != null)|| left_root.val != right_root.val) { return false; } else { if (left_root.left != null && right_root.right != null) { isOutsideSame = isSame(left_root.left, right_root.right); } else if (left_root.left == null && right_root.right == null) { isOutsideSame = true; } if (left_root.right != null && right_root.left != null) { isInsideSame = isSame(left_root.right, right_root.left); } else if (left_root.right == null && right_root.left == null) { isInsideSame = true; } } return isOutsideSame && isInsideSame; }
阅读全文
0 0
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- 对称的二叉树
- Numerical Computing with MaTLAB 中的工具箱 ncm 以及习题答案和程序下载
- ssh登录目标服务器,环境变量传播问题
- 整数中1出现的次数(从1到n整数中1出现的次数)java实现
- 安卓开发GridView添加头布局和
- 1004. 成绩排名
- 对称的二叉树
- 【数组变化检测】注意事项
- PHP 常用的header头部定义汇总
- Python 基础
- RPM中国镜像
- 配置AndroidStudio
- scrapy安装
- 图灵测试小游戏
- Kali Linux安装教程--VirtualBox下的系统优化