R8-二叉树篇
思路:
DFS搜索+递归分解成子问题逐步判断即可。
java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return root==null || recur(root.left,root.right);
}
boolean recur(TreeNode L,TreeNode R){
if (L==null && R==null) return true;
if (L==null || R==null || L.val!=R.val) return false;
return recur(L.left,R.right)&&recur(L.right,R.left);
}
}
真-没落的java