博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Symmetric Tree
阅读量:4074 次
发布时间:2019-05-25

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

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? 

Java代码:

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

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

你可能感兴趣的文章
Android framework中修改或者添加资源无变化或编译不通过问题详解
查看>>
linux怎么切换到root里面?
查看>>
linux串口操作及设置详解
查看>>
安装alien,DEB与RPM互换
查看>>
编译Android4.0源码时常见错误及解决办法
查看>>
Android 源码编译make的错误处理
查看>>
linux环境下C语言中sleep的问题
查看>>
ubuntu 12.04 安装 GMA3650驱动
查看>>
新版本的linux如何生成xorg.conf
查看>>
xorg.conf的编写
查看>>
启用SELinux时遇到的问题
查看>>
virbr0 虚拟网卡卸载方法
查看>>
No devices detected. Fatal server error: no screens found
查看>>
新版本的linux如何生成xorg.conf
查看>>
virbr0 虚拟网卡卸载方法
查看>>
Centos 6.0_x86-64 终于成功安装官方显卡驱动
查看>>
Linux基础教程:CentOS卸载KDE桌面
查看>>
db sql montior
查看>>
read humor_campus
查看>>
IBM WebSphere Commerce Analyzer
查看>>