leetcode刷题14天
100. Same Tree
Given two binary trees, write a function to check if they are the same or not.

Two binary trees are considered the same if they are structurally identical and the nodes have the same value.
leetcode笔记 100. Same Tree & 101. Symmetric Tree

代码:

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;
        else
        	return isSameTree(p.left,q.left)&&isSameTree(p.right,q.right);
    }
}
  1. Symmetric Tree
    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

For example, this binary tree [1,2,2,3,4,4,3] is symmetric:
leetcode笔记 100. Same Tree & 101. Symmetric Tree
代码:

class Solution {
    public  boolean isSymmetric(TreeNode p,TreeNode q) {
    	if(p==null&&q==null)
    		return true;
    	if(p==null||q==null)
    		return false;
    	if(p.val == q.val)
    		return isSymmetric(p.left,q.right)&&isSymmetric(p.right,q.left);
    	else 
    		return false;
        
    }	
    public boolean isSymmetric(TreeNode root) {
    	return isSymmetric(root,root);   
    }
}

相关文章:

  • 2022-03-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
猜你喜欢
  • 2021-10-01
  • 2021-08-30
  • 2021-11-09
  • 2021-11-29
  • 2021-06-23
  • 2021-07-30
  • 2021-11-28
相关资源
相似解决方案