zoukankan      html  css  js  c++  java
  • [leedcode 101] Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

    For example, this binary tree is symmetric:

        1
       / 
      2   2
     /  / 
    3  4 4  3
    

    But the following is not:

        1
       / 
      2   2
          
       3    3
    /**
     * 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) {
            /*本题题意验证一个二叉树是不是镜像的,使用递归思想
            如果两个根节点相等,递归验证镜像的对应部分isSymm(left.left,right.right)&&isSymm(left.right,right.left);
            注意自定义函数的意义:两个参数,代表镜像的两个对应点
            */
            if(root==null) return true;
            return isSymm(root.left,root.right);
        }
        public boolean isSymm(TreeNode left,TreeNode right){
            /*if(left==null) return right==null?true:false;
            if(right==null) return false;
            */
            if(left==null&&right==null) return true;
            if(left==null||right==null) return false;
            if(left.val==right.val){
                return isSymm(left.left,right.right)&&isSymm(left.right,right.left);
            }
            return false;
        }
    }
  • 相关阅读:
    将node.js代码放到阿里云上,并启动提供外部接口供其访问
    Linux内核深度解析之内核互斥技术——读写信号量
    man 1 2 3 4...
    Android Sepolicy 相关工具
    selinux misc
    ext4 mount options
    tune2fs cmd(ext fs)
    /dev/tty node
    kernel misc
    fork & vfork
  • 原文地址:https://www.cnblogs.com/qiaomu/p/4661932.html
Copyright © 2011-2022 走看看