博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode: Symmetric Tree 解题报告
阅读量:6901 次
发布时间:2019-06-27

本文共 3601 字,大约阅读时间需要 12 分钟。

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.

SOL 1 & 2:

两个解法,递归和非递归.

1 /** 2  * Definition for binary tree 3  * public class TreeNode { 4  *     int val; 5  *     TreeNode left; 6  *     TreeNode right; 7  *     TreeNode(int x) { val = x; } 8  * } 9  */10 public class Solution {11     // solution 1:12     public boolean isSymmetric(TreeNode root) {13         if (root == null) {14             return true;15         }16         17         return isSymmetricTree(root.left, root.right);18     }19     20     /*21      * 判断两个树是否互相镜像 22         (1) 根必须同时为空,或是同时不为空23      * 24      * 如果根不为空: 25         (1).根的值一样 26         (2).r1的左树是r2的右树的镜像 27         (3).r1的右树是r2的左树的镜像28      */29     public boolean isSymmetricTree1(TreeNode root1, TreeNode root2) {30         if (root1 == null && root2 == null) {31             return true;32         }33         34         if (root1 == null || root2 == null) {35             return false;36         }37         38         return root1.val == root2.val && 39              isSymmetricTree(root1.left, root2.right) && 40              isSymmetricTree(root1.right, root2.left);41     }42     43     // solution 2:44     public boolean isSymmetricTree(TreeNode root1, TreeNode root2) {45         if (root1 == null && root2 == null) {46             return true;47         }48         49         if (root1 == null || root2 == null) {50             return false;51         }52         53         Stack
s1 = new Stack
();54 Stack
s2 = new Stack
();55 56 // 一定记得初始化57 s1.push(root1);58 s2.push(root2);59 60 while (!s1.isEmpty() && !s2.isEmpty()) {61 TreeNode cur1 = s1.pop();62 TreeNode cur2 = s2.pop();63 64 if (cur1.val != cur2.val) {65 return false;66 }67 68 if (cur1.left != null && cur2.right != null) {69 s1.push(cur1.left);70 s2.push(cur2.right);71 } else if (!(cur1.left == null && cur2.right == null)) {72 return false;73 }74 75 if (cur1.right != null && cur2.left != null) {76 s1.push(cur1.right);77 s2.push(cur2.left);78 } else if (!(cur1.right == null && cur2.left == null)) {79 return false;80 }81 }82 83 return true;84 }85 }
View Code

 

2015.1.20:

简化了递归的解法,root与root本身是一对对称树。

1 /** 2  * Definition for binary tree 3  * public class TreeNode { 4  *     int val; 5  *     TreeNode left; 6  *     TreeNode right; 7  *     TreeNode(int x) { val = x; } 8  * } 9  */10 public class Solution {11     public boolean isSymmetric(TreeNode root) {12         return isMirror(root, root);13     }14     15     public boolean isMirror(TreeNode r1, TreeNode r2) {16         if (r1 == null && r2 == null) {17             return true;18         } else if (r1 == null || r2 == null) {19             return false;20         }21         22         return r1.val == r2.val23              && isMirror(r1.left, r2.right)24              && isMirror(r1.right, r2.left);25     }26 }
View Code

 

GITHUB:

转载地址:http://wopdl.baihongyu.com/

你可能感兴趣的文章
全美五大最创新的公司
查看>>
传微软将于明年推出Linux版Office
查看>>
后LHC时代对撞机:瞬间输出能量超全球电力千倍
查看>>
Leetcode#75Sort Colorsetcode
查看>>
3月30日作业
查看>>
公司电话突然不能打外线故障处理过程
查看>>
Windows Server 2008流媒体服务器---创建播放列表
查看>>
centos添加批量添加ip提示无效参数
查看>>
PHP mkdir函数
查看>>
Linux基础命令---检查密码文件pwck
查看>>
python这+=和=的拓展知识
查看>>
oracle集群件
查看>>
linux shell 中"2>&1"含义
查看>>
oracle 11g RAC grid安装前准备
查看>>
01背包 暴力搜索
查看>>
RIP区域和OSPF区域通信
查看>>
MySQL
查看>>
k3cloud开发环境引入dll与net源代码
查看>>
网络安全系列之四十 在Linux中设置SET位权限
查看>>
SCCM OSD部署排错
查看>>