1. import java.util.Stack;
  2. /**
  3. * Source : https://oj.leetcode.com/problems/symmetric-tree/
  4. *
  5. *
  6. * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
  7. *
  8. * For example, this binary tree is symmetric:
  9. *
  10. * 1
  11. * / \
  12. * 2 2
  13. * / \ / \
  14. * 3 4 4 3
  15. *
  16. * But the following is not:
  17. *
  18. * 1
  19. * / \
  20. * 2 2
  21. * \ \
  22. * 3 3
  23. *
  24. * Note:
  25. * Bonus points if you could solve it both recursively and iteratively.
  26. */
  27. public class SymmetricTree {
  28. /**
  29. * 判断一棵树是否是镜像对称的
  30. *
  31. * 类比判断两棵树是否相同,将一棵树leftNode,rightNode看做两棵树,判断两棵树是否是镜像对称的
  32. *
  33. * 根节点都为空,true
  34. * 只有一个根节点为空,false
  35. * 两个根节点都不为空且相同,递归判断两个子节点
  36. *
  37. * @param left
  38. * @param right
  39. * @return
  40. */
  41. public boolean isSymmetric (TreeNode left, TreeNode right) {
  42. if (left == null && right == null) {
  43. return true;
  44. }
  45. if ((left == null && right != null) || (left != null && right == null)) {
  46. return false;
  47. }
  48. if (left.value != right.value) {
  49. return false;
  50. }
  51. return isSymmetric(left.leftChild, right.rightChild) && isSymmetric(left.rightChild, right.leftChild);
  52. }
  53. /**
  54. * 使用递归判断是否是镜像对称的
  55. *
  56. * 借助栈实现,
  57. *
  58. * @param left
  59. * @param right
  60. * @return
  61. */
  62. public boolean isSymmetricByIterator (TreeNode left, TreeNode right) {
  63. Stack<TreeNode> leftStack = new Stack<TreeNode>();
  64. Stack<TreeNode> rightStack = new Stack<TreeNode>();
  65. leftStack.push(left);
  66. rightStack.push(right);
  67. while (leftStack.size() > 0 && rightStack.size() > 0) {
  68. TreeNode leftNode = leftStack.pop();
  69. TreeNode rightNode = rightStack.pop();
  70. if (leftNode == null && rightNode == null) {
  71. continue;
  72. }
  73. if ((leftNode == null && rightNode != null) || (leftNode != null && rightNode == null)) {
  74. return false;
  75. }
  76. if (leftNode.value != rightNode.value) {
  77. return false;
  78. }
  79. leftStack.push(leftNode.leftChild);
  80. leftStack.push(leftNode.rightChild);
  81. rightStack.push(rightNode.rightChild);
  82. rightStack.push(rightNode.leftChild);
  83. }
  84. return true;
  85. }
  86. public TreeNode createTree (char[] treeArr) {
  87. TreeNode[] tree = new TreeNode[treeArr.length];
  88. for (int i = 0; i < treeArr.length; i++) {
  89. if (treeArr[i] == '#') {
  90. tree[i] = null;
  91. continue;
  92. }
  93. tree[i] = new TreeNode(treeArr[i]-'0');
  94. }
  95. int pos = 0;
  96. for (int i = 0; i < treeArr.length && pos < treeArr.length-1; i++) {
  97. if (tree[i] != null) {
  98. tree[i].leftChild = tree[++pos];
  99. if (pos < treeArr.length-1) {
  100. tree[i].rightChild = tree[++pos];
  101. }
  102. }
  103. }
  104. return tree[0];
  105. }
  106. private class TreeNode {
  107. TreeNode leftChild;
  108. TreeNode rightChild;
  109. int value;
  110. public TreeNode(int value) {
  111. this.value = value;
  112. }
  113. public TreeNode() {
  114. }
  115. }
  116. public static void main(String[] args) {
  117. SymmetricTree symmetricTree = new SymmetricTree();
  118. char[] treeArr1 = new char[]{'1','2','2','3','4','4','3'};
  119. char[] treeArr2 = new char[]{'1','2','2','#','4','#','4'};
  120. TreeNode tree1 = symmetricTree.createTree(treeArr1);
  121. TreeNode tree2 = symmetricTree.createTree(treeArr2);
  122. System.out.println(symmetricTree.isSymmetric(tree1.leftChild, tree1.rightChild) + "----true");
  123. System.out.println(symmetricTree.isSymmetricByIterator(tree1.leftChild, tree1.rightChild) + "----true");
  124. System.out.println(symmetricTree.isSymmetric(tree2.leftChild, tree2.rightChild) + "----false");
  125. System.out.println(symmetricTree.isSymmetricByIterator(tree2.leftChild, tree2.rightChild) + "----false");
  126. }
  127. }

leetcode — symmetric-tree的更多相关文章

  1. LeetCode: Symmetric Tree 解题报告

    Symmetric Tree Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its ...

  2. [LeetCode] Symmetric Tree 判断对称树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  3. [leetcode]Symmetric Tree @ Python

    原题地址:https://oj.leetcode.com/problems/symmetric-tree/ 题意:判断二叉树是否为对称的. Given a binary tree, check whe ...

  4. LeetCode——Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  5. 二叉树系列 - [LeetCode] Symmetric Tree 判断二叉树是否对称,递归和非递归实现

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  6. [Leetcode] Symmetric tree 对称二叉树

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  7. Python3解leetcode Symmetric Tree

    问题描述: Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). ...

  8. LeetCode() Symmetric Tree

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  9. leetcode:Symmetric Tree【Python版】

    #error caused by:#1:{} 没有考虑None输入#2:{1,2,2} 没有控制h和t#3:{4,-57,-57,#,67,67,#,#,-97,-97} 没有考虑负号,将s从str变 ...

  10. 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree <span style="font-size:18px;"><span style="font-size:18px; ...

随机推荐

  1. eclipse自身导致的项目问题:上边提示需要移除无用包,下边类提示需要导入包。

  2. RSP小组——消消乐

    RSP小组--消消乐 团队所有博客总结 1.团队第一周作业 2.团队第二周作业 3.RSP小组--团队冲刺博客一 4.RSP小组--团队冲刺博客二 5.RSP小组--团队冲刺博客三 6.RSP小组-- ...

  3. Centos 7安装python3(PY3.6)

    # 安装 sudo yum install centos-release-scl sudo yum install rh-python36 #开启 scl enable rh-python36 bas ...

  4. c# asp.net mvc使用斑马GK888t打印机打印标签

    前言 c#语言,asp.net mvc,南京都昌电子病历模板工具(类似word),斑马GK888t,打印手腕带和标签纸. 实现步骤为:在页面上显示一个或多个都昌模板工具,点击页面上的button,出现 ...

  5. Android SQLite数据库升级,怎么做(事物更改)

    SQLiteOpenHelper // 如果数据库文件不存在,只有onCreate()被调用(该方法在创建数据库时被调用一次) public abstract void onCreate(SQLite ...

  6. typeof获取变量的数据类型 javascript

    获取变量的数据类型:typeof <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  7. 漏测BUG LIst

    5. 接口设计问题 -  主从存在延时,当两个接口需要一个主库,一个从库的时候,可能会出问题,时时性 4. 开发的接口文档也得进行简单的测试,根据产品文档/业务测试接口(针对问题2) 3. 需要上的课 ...

  8. centos 踩坑集锦

    定时任务 top 命令添加定时任务无效 我通过以下命令获取总进程数与僵尸进程数 vim procs.sh procs_total=`/bin/top -n 1|grep Tasks|sed 's/,/ ...

  9. VUE 出现Access to XMLHttpRequest at 'http://192.168.88.228/login/Login?phone=19939306484&password=111' from origin 'http://localhost:8080' has been blocked by CORS policy: The value of the 'Access-Contr

    报错如上图!!!!    解决办法首先打开 config -> index.js ,粘贴 如下图代码,'https://www.baidu.com'换成要访问的的api域名,注意只要域名就够了, ...

  10. JAVA---MYSQL 基本知识点 第二部分

    增删改查 (CRUD):   数据库  , 表  , 记录  ;   约束 ; 主键约束 :primary key  如果是int类型 可以使用 自动增长型  auto_increment; 唯一约束 ...