[leetcode]101. Symmetric Tree对称树
发布时间:2020-12-14 04:18:02 所属栏目:大数据 来源:网络整理
导读:Given a binary tree,check whether it is a mirror of itself (ie,symmetric around its center). For example,this binary tree? [1,2,3,4,3] ?is symmetric: 1 / 2 2 / / 3 4 4 3 ? But the following? [1,null,3] ?is not: 1 / 2 2 3 3 ? Note: Bonu
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 ? But the following? 1 / 2 2 3 3 ? Note: ? 题目 给定二叉树,判断其是否左右对称 ? 思路 DFS ? 代码 1 /* 2 Time:O(n), 3 Space: O(logn) 即 O(h) 树的deepest hight 4 */ 5 public class Solution { 6 public boolean isSymmetric(TreeNode root) { 7 if (root == null) return true; 8 return isSymmetric(root.left,root.right); 9 } 10 private static boolean isSymmetric(TreeNode p,TreeNode q) { 11 if (p == null && q == null) return true; // 终止条件 12 if (p == null || q == null) return false; // 终止条件 13 return p.val == q.val // 三方合并 14 && isSymmetric(p.left,q.right) 15 && isSymmetric(p.right,q.left); 16 } 17 } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在Delphi的属性 property中使用数组
- delphi – 与TBCDField和TfmtBCDfield以及oracle兼容性类型
- Delphi实现控件的移动、改变大小
- perl 报错 Sorry, we have to rerun the configuration dia
- 理解 Delphi 的类(十一) - 深入类中的方法[6] - 类中的方法
- delphi – 网站与客户端应用程序交谈?
- xmake 描述语法和作用域详解
- Delphi 中的字符串函数(6) - StrUtils 中的 Ansi 字符串函数
- vb.net – 如何在没有边框的Windows窗体上获得Aero Glass?
- 新的Delphi XE2自动生成的内部版本号是否链接到1.1.2000 00