GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — CodingInterview/Solution28.java at master · legolas007/CodingInterview

Expand file tree

/

Solution28.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
24 lines (22 loc) · 848 Bytes
/

Solution28.java

Copy path

File metadata and controls

24 lines (22 loc) · 848 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package com.usher.algorithm.offer;
/**
* @Author: Usher
* @Description:
* 对称的二叉树
* 判断一颗二叉树是不是对称的,如果一颗二叉树和它的镜像一样,那么就是对称的
*
*/
public class Solution28 {
boolean isSymmetrical(TreeNode pRoot) {
return pRoot == null || isEqual(pRoot.left, pRoot.right);
}
//先序遍历,递归实现。先判断根节点是否相等,然后在判断左右子树是否相等。
private boolean isEqual(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null)
return true;
if (t2 == null || t1 == null)
return false;
//如果都不为空,则 需要比较左右子树后,再根据比较结果断定
return t1.val == t2.val && isEqual(t1.left, t2.right) && isEqual(t1.right, t2.left);
}
}
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.