博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode OJ - Symmetric Tree && Same Tree
阅读量:6815 次
发布时间:2019-06-26

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

这两道题,大同小异。 我都是用BFS,在遍历的过程,判断结构是否相同/对称,值是否相同。

下面是AC代码:

1 /**  2      * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).  3      * @param root  4      * @return  5      */  6     public boolean isSymmetricRecursively(TreeNode root){  7         if(root == null)  8             return true;  9         return isSymmetricR(root.left, root.right); 10     } 11     /** 12      * 采用迭代的方式,输入的是两个对称位置的节点,如果以这两个节点为root的子树是对称的,则 13      * 左边的左子树和右边的右子树相同,且左边的右子树和右边的左子树相同,且这两个子树的树根的值相等 14      * @param left 15      * @param right 16      * @return 17      */ 18     private boolean isSymmetricR(TreeNode left, TreeNode right){ 19         if(left== null && right == null) 20             return true; 21         if(left == null && right!=null || right == null && left!=null) 22             return false; 23         if(left.left == null && left.right == null && 24                 right.left == null && right.right == null) 25             return left.val == right.val; 26         boolean f1 = (left.val == right.val); 27         boolean l1 = isSymmetricR(left.left, right.right); 28         boolean r1 = isSymmetricR(left.right, right.left); 29         return f1&&l1&&r1; 30          31     } 32     /** 33      * Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). 34      * solve it iteratively 35      * @param root 36      * @return 37      */ 38     public boolean isSymmetricIteratively(TreeNode root){ 39         if(root == null || (root.left == null && root.right == null)) 40             return true; 41         LinkedList
qleft = new LinkedList
(); 42 LinkedList
qright = new LinkedList
(); 43 44 if(root.left!=null && root.right == null || 45 root.right!=null && root.left == null) 46 return false; 47 qleft.offer(root.left); 48 qright.offer(root.right); 49 50 while(!qleft.isEmpty() && !qright.isEmpty()){ 51 TreeNode left = qleft.poll(); 52 TreeNode right = qright.poll(); 53 54 if(left.val != right.val) 55 return false; 56 57 if(left.right != null && right.left!=null){ 58 qleft.offer(left.right); 59 qright.offer(right.left); 60 }else if(left.right!= null && right.left == null || 61 left.right == null && right.left !=null) 62 return false; 63 64 if(right.right != null && left.left!=null){ 65 qleft.offer(left.left); 66 qright.offer(right.right); 67 }else if(right.right!= null && left.left == null || 68 right.right == null && left.left !=null) 69 return false; 70 } 71 return true; 72 } 73 /** 74 * by BFS, to check if they are equal or not 75 * Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 76 * @param p 77 * @param q 78 * @return 79 */ 80 public boolean isSameTree(TreeNode p, TreeNode q){ 81 if(p == null && q == null) 82 return true; 83 if(p == null && q!=null || p!=null && q==null) 84 return false; 85 LinkedList
pqueue = new LinkedList
(); 86 LinkedList
qqueue = new LinkedList
(); 87 88 pqueue.offer(p); 89 qqueue.offer(q); 90 91 while(!pqueue.isEmpty() && !qqueue.isEmpty()){ 92 TreeNode pC = pqueue.poll(); 93 TreeNode qC = qqueue.poll(); 94 //have same value 95 if(pC.val!=qC.val) 96 return false; 97 //structurally identical or not 98 if(pC.left!=null && qC.left == null || 99 pC.left == null && qC.left!=null)100 return false;101 else if(pC.left!=null && qC.left!=null){102 pqueue.offer(pC.left);103 qqueue.offer(qC.left);104 }105 106 if(pC.right == null && qC.right != null || 107 pC.right!=null && qC.right == null)108 return false;109 else if(pC.right!=null && qC.right!=null){110 pqueue.offer(pC.right);111 qqueue.offer(qC.right);112 }113 }114 /**115 * there is no need116 * if(pqueue.isEmpty() && !qqueue.isEmpty() ||117 qqueue.isEmpty() && !pqueue.isEmpty())118 return false;119 */120 return true;121 }

 

转载于:https://www.cnblogs.com/echoht/p/3708024.html

你可能感兴趣的文章
Linux命令小记
查看>>
基于ROS和beaglebone的串口通信方式,使用键盘控制移动机器人
查看>>
android.view.WindowLeaked的解决办法
查看>>
存储过程的笔记
查看>>
OpenCV学习(27) 直方图(4)
查看>>
深度学习原理与框架-Tensorflow基本操作-实现线性拟合
查看>>
[leetcode-168-Excel Sheet Column Title]
查看>>
SpringBoot和数据库连接
查看>>
二叉搜索树
查看>>
网页小技巧-360doc个人图书馆复制文字
查看>>
delete删除-some
查看>>
maven阿里云中央仓库
查看>>
15.12.14listbox列表框
查看>>
sql 行转列
查看>>
(转)Python新手写出漂亮的爬虫代码1——从html获取信息
查看>>
配置Nim的默认编译参数 release build并运行
查看>>
图片下载
查看>>
《构建之法》第四章读后感
查看>>
python os.path.dirname()
查看>>
android 解析json数据格式
查看>>