zoukankan      html  css  js  c++  java
  • Same Tree

    package cn.edu.xidian.sselab;
    /**
     * title:Same Tree
     * content:
     *  Given two binary trees, write a function to check if they are equal or not.
     *  Two binary trees are considered equal if they are structurally identical and the nodes have the same value.
     */
    public class SameTree {

        /**
         * @author wzy
         * @param args
         * @see 判断两棵树是否相同,不仅要看结构,同时也要看每个节点对应的值
         * 首先想到的是对树分别进行遍历,遍历想到了递归
         * 遍历采用的是后序遍历的方式,同时遍历都叶子节点之后要考虑四种情况
         * (1)如果对应的节点都是null,则返回true
         * (2)如果其中一个节点为null,另外一个节点不为null,则返回false
         * (3)如果两个节点都不为null,但是数值不相等 ,返回false
         * (4)如果两个节点都不为null,而且数值相等,则进行递归操作
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub

        }
        public class TreeNode{
            int val;
            TreeNode left;
            TreeNode right;
            TreeNode(int x){
                this.val = x;
            }
        }
        public boolean isSameTree(TreeNode p, TreeNode q){
            boolean flag1 = false;
            boolean flag2 = false;        
            if(p == null && q == null ){
                return true;
            }else if((p == null || q == null)){
                return false;
            }else if(p.val != q.val){
                return false;            
            }else{
                flag1 = isSameTree(p.left,q.left);
                flag2 = isSameTree(p.right,q.right);
            }
            return (flag1 && flag2);
        }
            
    }

  • 相关阅读:
    单线程的JavaScript是如何实现异步的
    前端优化之 -- 使用 require.context 让项目实现路由自动导入
    插入排序
    选择排序
    冒泡排序
    强缓存和协商缓存
    ES6 Set求两个数组的并集、交集、差集;以及对数组去重
    实现一个new操作符
    我理解的浅拷贝和深拷贝
    javascript专题系列--js乱序
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/4937618.html
Copyright © 2011-2022 走看看