标签:val off empty || pre ica tac subject 取数
public class Solution { boolean isSymmetrical(TreeNode pRoot) { if(pRoot == null) { return true; } return judge(pRoot.left,pRoot.right); } private boolean judge(TreeNode left, TreeNode right) { if(left == null && right == null) return true; if(left == null || right == null) return false; if(left.val != right.val) return false; return judge(left.left,right.right) && judge(left.right,right.left); } }
思路二:用栈来成对存储左右子树 并且取得时候成对取 这样就保证比较的是镜像元素
用queue队列来存取数据也可以
import java.util.Stack; 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; } }
标签:val off empty || pre ica tac subject 取数
原文地址:https://www.cnblogs.com/nlw-blog/p/12466807.html