Symmetric Tree 判断树是不是对称的
来源:互联网 发布:金融网络聊天技巧 编辑:程序博客网 时间:2024/05/21 14:51
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 / \ 2 2 / \ / \3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1 / \ 2 2 \ \ 3 3
判断树是不是对称,即判断它的左右子树是不是对称的。
我的方法是:
1. 镜像右子树。
2. 判断镜像右子树和左子树是不是相同的。
运行时间:
代码:
public class SymmetricTree { public boolean isSymmetric(TreeNode root) { if (root == null) { return true; } return isSameTree(root.left, doMirror(root.right)); } public TreeNode doMirror(TreeNode root) { if (root == null) { return root; } TreeNode temp = doMirror(root.left); root.left = doMirror(root.right); root.right = temp; return root; } public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) { return true; } else if (p == null || q == null) { return false; } else { return p.val == q.val && isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } }}
1 0
- Symmetric Tree 判断树是不是对称的
- Symmetric Tree 是不是对称树
- Symmetric Tree 判断对称的树
- leetcode Symmetric Tree对称树的判断
- 判断二叉树是否是水平对称的 Symmetric Tree
- LeetCode(Symmetric Tree)判断二叉树是否是对称的
- 二叉树镜像,对称二叉树的判断-symmetric Tree
- 【leetcode 二叉树对称判断】Symmetric Tree
- 判断树是否对称 101. Symmetric Tree
- Symmetric Tree 对称树
- Leetcode-Symmetric Tree——判断二叉树是否对称
- 101. Symmetric Tree (判断二叉树是否对称)
- 101. Symmetric Tree--判断是否为对称树
- 101Symmetric Tree判断对称二叉树dfs
- LeetCode-101-Symmetric Tree(判断是否为对称树)
- 017-101-Symmetric Tree 判断树是否对称
- Symmetric Tree 判断一棵树是否对称
- leetcode-Symmetric Tree 对称树
- oracle 创建dblink访问远程数据库
- panda3d中文网文档翻译
- python权威指南
- mysql查询语句in和exists二者的区别和性能影响
- 探索友元的继承和传递问题
- Symmetric Tree 判断树是不是对称的
- 去除 UItableview headerview黏性(sticky)
- Android之Service(二):服务的基本用法
- Kafka0.10.0.0单节点-多代理集群
- 纯虚函数能为private吗?
- 一些C/C++中的函数
- leetcode 32. Longest Valid Parentheses
- Java题集
- net-snmp中载入第三方mib库