zoukankan      html  css  js  c++  java
  • 【LeetCode】Symmetric Tree 推断一棵树是否是镜像的

    题目:Symmetric Tree

    <span style="font-size:18px;"><span style="font-size:18px;">/**LeetCode Symmetric Tree 对称的树
     * 思路:推断一棵树是否对称,1.有左子树就要有右子树
     * 						2.除根节点外对称节点值要同样
     * 注意:对称后就是左子树的左节点和右子树的右节点比較
    	 * Definition for binary tree
    	 * public class TreeNode {
    	 *     int val;
    	 *     TreeNode left;
    	 *     TreeNode right;
    	 *     TreeNode(int x) { val = x; }
    	 * }
    	 */
    package javaTrain;
    
    public class Train8 { 
    	    public boolean isSymmetric(TreeNode root) { 
    	    	if(root == null) return true;
    	    	if(root.left == null && root.right == null) return true;
    	    	else if(root.left == null || root.right == null) return false;
    	    	return help(root.left,root.right);
    	    }
    	    private boolean help(TreeNode left,TreeNode right){
    	    	if(left == null && right == null) return true;
    	    	else if(left == null || right == null) return false;
    	    	if(left.val == right.val)
    	    		return help(left.left,right.right ) && help(left.right,right.left);
    	    	else return false;
    	    }
    }
    </span></span>


     

  • 相关阅读:
    Java:多线程<一>
    Java:Exception
    Java: 内部类
    Ubuntu安装jdk
    ubuntu搜狗拼音安装
    录音-树莓派USB摄像头话筒
    leetcode 最小栈
    leetcode 编辑距离 动态规划
    leetcode 最小覆盖字串
    leetcode 最长上升子序列 动态规划
  • 原文地址:https://www.cnblogs.com/yxwkf/p/5280598.html
Copyright © 2011-2022 走看看