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

    简介

    如何判断一颗对称二叉树, 最本质的特点就是左右子树是否相等. 递归遍历即可,注意终止条件的添加

    code

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        bool check(TreeNode *p, TreeNode *q){
            if(!p && !q) return true;
            if(!p || !q) return false;
            return p->val == q->val && check(p->left, q->right) && check(p->right, q->left);
        }
        bool isSymmetric(TreeNode* root) {
            return check(root, root);
        }
    };
    
    class Solution {
        boolean check(TreeNode p, TreeNode q){
            if(p == null && q == null) return true;
            if(p == null || q == null) return false;
            return p.val == q.val && check(p.left, q.right) && check(p.right, q.left);
        }
        public boolean isSymmetric(TreeNode root) {
            return check(root, root);
        }
    }
    
    Hope is a good thing,maybe the best of things,and no good thing ever dies.----------- Andy Dufresne
  • 相关阅读:
    FastDfs安装
    groovy学习(二)
    jenkins(一)jenkins使用教程
    jenkins(二)Pipeline语法速成
    groovy学习(一)
    Docker笔记(一)
    运维笔记(一)
    Maven管理(一)
    工厂模式案例与理解
    python 探測端口
  • 原文地址:https://www.cnblogs.com/eat-too-much/p/14801605.html
Copyright © 2011-2022 走看看