zoukankan      html  css  js  c++  java
  • 对称二叉树

    一、题目描述

    请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。例如下面的二叉树:

     思路:如果根节点为null,返回true。然后判断左子树和右子树是否对称。

    二、代码演示

    public class Solution {
        boolean isSymmetrical(TreeNode pRoot)
        {
            if(pRoot == null) return true;
            return search(pRoot.left,pRoot.right);
        }
        
        boolean search(TreeNode node1,TreeNode node2) {
            if(node1 == null && node2 == null) return true;
            if(node1 == null && node2 != null || node1 != null && node2 == null) return false;
            if(node1.val != node2.val) return false;
            return search(node1.left,node2.right) && search(node1.right,node2.left);
        }
    }
  • 相关阅读:
    字典树
    Floyd算法
    迪杰斯特拉算法---单源点最短路径
    二叉树的遍历
    图的遍历
    二叉排序树
    拓扑排序
    开发中框架的发展
    IOC
    JS操作JSON总结
  • 原文地址:https://www.cnblogs.com/neuzk/p/9504099.html
Copyright © 2011-2022 走看看