LeetCode - Easy - 100. Same Tree

Topic

Tree, Depth-first Search

Description

https://leetcode.com/problems/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.

Example 1:

Input:     1         1
          / \       / \
         2   3     2   3

        [1,2,3],   [1,2,3]

Output: true

Example 2:

Input:     1         1
          /           \
         2             2

        [1,2],     [1,null,2]

Output: false

Example 3:

Input:     1         1
          / \       / \
         2   1     1   2

        [1,2,1],   [1,1,2]

Output: false

Analysis

方法一:递归前序遍历

方法二:非递归前序遍历

Submission

import java.util.Arrays;
import java.util.LinkedList;

import com.lun.util.BinaryTree.TreeNode;

public class SameTree {

	//方法一:递归前序遍历
    public boolean isSameTree1(TreeNode p, TreeNode q) {
        if (p == null && q != null || p != null && q == null)
			return false;

		if (p != null && q != null) {
			if (p.val != q.val) {
				return false;
			}

			// 判断左右子树
			return isSameTree1(p.left, q.left) && isSameTree1(p.right, q.right);
		}

		return true;
    }
    
    //方法二:非递归前序遍历
    public boolean isSameTree2(TreeNode root1, TreeNode root2) {
    	TreeNode p = root1, q = root2;
    	
    	LinkedList<TreeNode> stack = new LinkedList<>();
    	
    	while(true) {
    		if(p == null && q != null || p != null && q == null) {
    			return false;
    		}
    		
    		if(p != null && q != null) {
    			if(p.val != q.val) {
    				return false;
    			}
    			
    			//下列push的精简
    			stack.addAll(0, Arrays.asList(p.left, q.left, p.right, q.right));
    			
//    			stack.push(q.right);
//    			stack.push(p.right);
//    			
//    			stack.push(q.left);
//    			stack.push(p.left);
    		}
    		
    		if(stack.isEmpty()) {
    			break;
    		}else {
    			p = stack.pop();
    			q = stack.pop();
    		}
    	}
    	
    	return true;
    }
    
}

Test

import static org.junit.Assert.*;
import org.junit.Test;

import com.lun.util.BinaryTree.TreeNode;

public class SameTreeTest {

	@Test
	public void test() {
		SameTree obj = new SameTree();
		
		TreeNode root1 = new TreeNode(1);
		root1.left = new TreeNode(2);
		root1.right = new TreeNode(3);
		
		TreeNode root2 = new TreeNode(1);
		root2.left = new TreeNode(2);
		root2.right = new TreeNode(3);
		
		assertTrue(obj.isSameTree1(root1, root2));
		assertTrue(obj.isSameTree2(root1, root2));
	}
	
	@Test
	public void test2() {
		SameTree obj = new SameTree();
		
		TreeNode root1 = new TreeNode(1);
		root1.left = new TreeNode(2);
		
		TreeNode root2 = new TreeNode(1);
		root2.right = new TreeNode(2);
		
		assertFalse(obj.isSameTree1(root1, root2));
		assertFalse(obj.isSameTree2(root1, root2));
	}
	
	@Test
	public void test3() {
		SameTree obj = new SameTree();
		
		TreeNode root1 = new TreeNode(1);
		root1.left = new TreeNode(2);
		root1.right = new TreeNode(1);
		
		TreeNode root2 = new TreeNode(1);
		root2.right = new TreeNode(1);
		root2.right = new TreeNode(2);
		
		assertFalse(obj.isSameTree1(root1, root2));
		assertFalse(obj.isSameTree2(root1, root2));
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值