zoukankan      html  css  js  c++  java
  • leetcode 100.相同的树

    package com.example.lettcode.dailyexercises;
    
    /**
     * @Class IsSameTree
     * @Description 100.相同的树
     * 给定两个二叉树,编写一个函数来检验它们是否相同。
     * 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
     * <p>
     * 示例 1:
     * 输入:
     * 1         1
     * /        / 
     * 2   3     2   3
     * <p>
     * [1,2,3],   [1,2,3]
     * <p>
     * 输出: true
     * <p>
     * 示例 2:
     * <p>
     * 输入:
     * 1          1
     * /           
     * 2             2
     * <p>
     * [1,2],     [1,null,2]
     * <p>
     * 输出: false
     * <p>
     * 示例 3:
     * 输入:
     * 1         1
     * /        / 
     * 2   1     1   2
     * <p>
     * [1,2,1],   [1,1,2]
     * <p>
     * 输出: false
     * @Author
     * @Date 2020/8/7
     **/
    public class IsSameTree {
        static class TreeNode {
            int val;
            TreeNode left;
            TreeNode right;
    
            TreeNode() {
            }
    
            TreeNode(int val) {
                this.val = val;
            }
    
            TreeNode(int val, TreeNode left, TreeNode right) {
                this.val = val;
                this.left = left;
                this.right = right;
            }
        }
    
        public static boolean isSameTree(TreeNode p, TreeNode q) {
            if (p == null) return q == null;
            if (q == null) return p == null;
            if (p.val != q.val) return false;
            boolean resL = isSameTree(p.left, q.left);
            boolean resR = isSameTree(p.right, q.right);
            return resL && resR;
        }
    
        public static void main(String[] args) {
            // [1,2,3],   [1,2,3]
            TreeNode rootP = new TreeNode(1);
            TreeNode treeNode2 = new TreeNode(2);
            TreeNode treeNode3 = new TreeNode(3);
            rootP.left = treeNode2;
            rootP.right = treeNode3;
            TreeNode rootQ = new TreeNode(1);
            treeNode2 = new TreeNode(2);
            treeNode3 = new TreeNode(3);
            rootQ.left = treeNode2;
            rootQ.right = treeNode3;
            boolean ans = isSameTree(rootP, rootQ);
            System.out.println("IsSameTree demo01 result:" + ans);
    
            // [1,2],     [1,null,2]
            rootP = new TreeNode(1);
            treeNode2 = new TreeNode(2);
            rootP.left = treeNode2;
            rootQ = new TreeNode(1);
            treeNode2 = new TreeNode(2);
            rootQ.right = treeNode2;
            ans = isSameTree(rootP, rootQ);
            System.out.println("IsSameTree demo02 result:" + ans);
    
            //[1,2,1],   [1,1,2]
            rootP = new TreeNode(1);
            treeNode2 = new TreeNode(2);
            treeNode3 = new TreeNode(1);
            rootP.left = treeNode2;
            rootP.right = treeNode3;
            rootQ = new TreeNode(1);
            treeNode2 = new TreeNode(1);
            treeNode3 = new TreeNode(2);
            rootQ.left = treeNode2;
            rootQ.right = treeNode3;
            ans = isSameTree(rootP, rootQ);
            System.out.println("IsSameTree demo03 result:" + ans);
        }
    }
    
  • 相关阅读:
    SharePoint 2013 配置我的网站 图文引导
    关于SharePoint REST中的授权的研究
    SharePoint重置密码功能Demo
    SharePoint 沙盒解决方案 VS 场解决方案
    移动设备和SharePoint 2013
    win32
    win32
    链表复习-1
    win32
    洛谷基础算法
  • 原文地址:https://www.cnblogs.com/fyusac/p/13450774.html
Copyright © 2011-2022 走看看