题解 | #对称的二叉树#非递归dfs实现
对称的二叉树
http://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
boolean isSymmetrical(TreeNode pRoot) {
if(pRoot == null){
return true;
}
Stack<TreeNode> stack = new Stack();
stack.push(pRoot.left);
stack.push(pRoot.right);
while(!stack.isEmpty()){
TreeNode right = stack.pop();
TreeNode left = stack.pop();
if(left == null && right == null){
continue;
}
if(left == null || right == null){
return false;
}
if(left.val != right.val){
return false;
}
stack.push(left.left);
stack.push(right.right);
stack.push(left.right);
stack.push(right.left);
}
return true;
}
}