标签:二叉树 bsp 对称树 href lan 思路 png nod .com
对称树/镜像树:关于轴对称,每个结点绕轴旋转180度后和原树相同
属于二叉树,原理同LeetCode 100.Same Tree,递归解决;
假设T1,T2是用一棵二叉树的两个引用:
返回true:T1、T2均为空或T1的左子树等于T2的右子树且T1的右子树等于T2的左子树;
返回false:T1不等于T2;
递归调用,T1==null&&T2==null为递归结束条件
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSymmetric(TreeNode root) { return root==null || judge(root, root); } public boolean judge(TreeNode t1, TreeNode t2){ if(t1==null && t2==null){ return true; } if(t1==null ||t2==null){ return false; } return (t1.val==t2.val)&&(judge(t1.left,t2.right))&&(judge(t1.right,t2.left)); } }
【LeetCode】101. Symmetric Tree-对称树/镜像树
标签:二叉树 bsp 对称树 href lan 思路 png nod .com
原文地址:http://www.cnblogs.com/WalkerSteve/p/6603097.html