zoukankan      html  css  js  c++  java
  • Same Tree

    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.

    /**
     * Definition for binary tree
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public boolean isSameTree(TreeNode p, TreeNode q) {
            if(p==null&&q==null) return true;
            if(p==null||q==null) return false;
            if(p.val!=q.val) return false;
            if(!isSameTree(p.left,q.left)) return false;
            if(!isSameTree(p.right,q.right)) return false;
            return true;
        }
    }
  • 相关阅读:
    audio_policy.conf说明(翻译)
    Qt
    linux C
    Linux C
    Linux C
    Qt
    Qt
    JSON
    JSON
    Qt
  • 原文地址:https://www.cnblogs.com/mrpod2g/p/4276901.html
Copyright © 2011-2022 走看看