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

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

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

     这道题也挺有意思的。不过首先要搞懂题目,首先搞清楚怎样的BTS才算symmetric的。必须要是一个balanced的BTS然后两边的height都相等。

     这道题个人觉得用recursive来写比较容易哈。

     代码如下。~

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public boolean isSymmetric(TreeNode root) {        if(root==null) return true;        return symhelper(root.left,root.right);    }        public boolean symhelper(TreeNode left,TreeNode right){        if(left==null||right==null){            return (left==right);        }        if((left.val==right.val)&&(symhelper(left.left,right.right))&&(symhelper(left.right,right.left))){            return true;        }else{            return false;        }    }}

 

转载于:https://www.cnblogs.com/orangeme404/p/4760652.html

你可能感兴趣的文章
Spring源码情操陶冶-PathMatchingResourcePatternResolver路径资源匹配溶解器
查看>>
C++数据结构大作业之大数加法、乘法、幂运算
查看>>
C++编程对缓冲区的理解
查看>>
windows下 安装 rabbitMQ 及操作常用命令
查看>>
Linux中 bash_profile和.bashrc的区别(启动文件)
查看>>
Tomcat出现java.lang.Exception: Socket bind failed
查看>>
AngularJS
查看>>
DBCP、C3P0、Proxool 、 BoneCP开源连接池的比较
查看>>
[.NET WebAPI系列01] WebAPI 简单例子
查看>>
[leetcode] Minimum Path Sum
查看>>
PAT乙级1021.个位数统计(15 分)
查看>>
强化学习Q-Learning算法详解
查看>>
Spring MVC
查看>>
winform treeview 复选框,父节点子节点联动Bug
查看>>
C#_委托类型以及Action/Fanc_2018Oct
查看>>
es数组去重的简写
查看>>
Training Logisches Denken
查看>>
谁分配谁释放
查看>>
正则表达式
查看>>
Java集合之LinkedHashSet源码分析
查看>>