# [剑指offer] 对称的二叉树

#### 参考代码

/*
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;

public TreeNode(int val) {
this.val = val;
}
}
*/
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if(pRoot == null)
return true;
return isSymmetrical(pRoot.left, pRoot.right);
}
boolean isSymmetrical(TreeNode left, TreeNode right){
if(left == null && right == null)
return true;
if(left == null || right == null)
return false;
if(left.val == right.val){
return isSymmetrical(left.left, right.right) &&
isSymmetrical(left.right, right.left);
}
return false;
}
}

import java.util.Stack;
public class Solution {
boolean isSymmetrical(TreeNode pRoot)
{
if(pRoot == null)
return true;
Stack<TreeNode> s = new Stack<TreeNode>();
s.push(pRoot.left);
s.push(pRoot.right);
while(!s.isEmpty()){
TreeNode right = s.pop();
TreeNode left = s.pop();
if(right == null && left == null)
continue;
if(right == null || left == null)
return false;
if(right.val != left.val)
return false;
s.push(left.left);
s.push(right.right);
s.push(left.right);
s.push(right.left);
}
return true;
}
}

110 篇文章26 人订阅

0 条评论

## 相关文章

772

882

### 剑指Offer-二叉树的镜像

package Tree; import java.util.ArrayDeque; import java.util.ArrayList; import j...

3384

### 剑指Offer-二叉树的深度

package Tree; import java.util.LinkedList; import java.util.Queue; /** * 二叉树...

3004

1492

### 剑指Offer-按之字形顺序打印二叉树

package Tree; import java.util.ArrayList; import java.util.LinkedList; import j...

2844

### LeetCode Maximum Depth of Binary Tree题目分析代码

Given a binary tree, find its maximum depth.

681

2243

### Java 通过先序中序序列生成二叉树

二叉树的前序以及后续序列，以空格间隔每个元素，重构二叉树，最后输出二叉树的三种遍历方式的序列以验证。

1901

### Leetcode 114 Flatten Binary Tree to Linked List

Given a binary tree, flatten it to a linked list in-place. For example, Given...

2588