码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode_100. Same Tree

时间:2019-09-28 09:14:13      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:class   tput   amp   ||   junit   java   sid   new t   ret   

 

100. Same Tree

Easy

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

 

package leetcode.easy;

import java.util.ArrayDeque;

/**
 * Definition for a binary tree node. public class TreeNode { int val; TreeNode
 * left; TreeNode right; TreeNode(int x) { val = x; } }
 */
class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;

	TreeNode(int x) {
		val = x;
	}
}

public class SameTree {
	@org.junit.Test
	public void test1() {
		TreeNode tn11 = new TreeNode(1);
		TreeNode tn12 = new TreeNode(2);
		TreeNode tn13 = new TreeNode(3);
		tn11.left = tn12;
		tn11.right = tn13;
		tn12.left = null;
		tn12.right = null;
		tn13.left = null;
		tn13.right = null;

		TreeNode tn21 = new TreeNode(1);
		TreeNode tn22 = new TreeNode(2);
		TreeNode tn23 = new TreeNode(3);
		tn21.left = tn22;
		tn21.right = tn23;
		tn22.left = null;
		tn22.right = null;
		tn23.left = null;
		tn23.right = null;

		System.out.println(isSameTree1(tn11, tn21));
		System.out.println(isSameTree2(tn11, tn21));
	}

	@org.junit.Test
	public void test2() {
		TreeNode tn11 = new TreeNode(1);
		TreeNode tn12 = new TreeNode(2);
		tn11.left = tn12;
		tn11.right = null;
		tn12.left = null;
		tn12.right = null;

		TreeNode tn21 = new TreeNode(1);
		TreeNode tn22 = new TreeNode(2);
		tn21.left = null;
		tn21.right = tn22;
		tn22.left = null;
		tn22.right = null;

		System.out.println(isSameTree1(tn11, tn21));
		System.out.println(isSameTree2(tn11, tn21));
	}

	@org.junit.Test
	public void test3() {
		TreeNode tn11 = new TreeNode(1);
		TreeNode tn12 = new TreeNode(2);
		TreeNode tn13 = new TreeNode(1);
		tn11.left = tn12;
		tn11.right = tn13;
		tn12.left = null;
		tn12.right = null;
		tn13.left = null;
		tn13.right = null;

		TreeNode tn21 = new TreeNode(1);
		TreeNode tn22 = new TreeNode(1);
		TreeNode tn23 = new TreeNode(2);
		tn21.left = tn22;
		tn21.right = tn23;
		tn22.left = null;
		tn22.right = null;
		tn23.left = null;
		tn23.right = null;

		System.out.println(isSameTree1(tn11, tn21));
		System.out.println(isSameTree2(tn11, tn21));
	}

	public boolean isSameTree1(TreeNode p, TreeNode q) {
		// p and q are both null
		if (p == null && q == null) {
			return true;
		}
		// one of p and q is null
		if (q == null || p == null) {
			return false;
		}
		if (p.val != q.val) {
			return false;
		}
		return isSameTree1(p.right, q.right) && isSameTree1(p.left, q.left);
	}

	public boolean check(TreeNode p, TreeNode q) {
		// p and q are null
		if (p == null && q == null) {
			return true;
		}
		// one of p and q is null
		if (q == null || p == null) {
			return false;
		}
		if (p.val != q.val) {
			return false;
		}
		return true;
	}

	public boolean isSameTree2(TreeNode p, TreeNode q) {
		if (p == null && q == null) {
			return true;
		}
		if (!check(p, q)) {
			return false;
		}

		// init deques
		ArrayDeque<TreeNode> deqP = new ArrayDeque<TreeNode>();
		ArrayDeque<TreeNode> deqQ = new ArrayDeque<TreeNode>();
		deqP.addLast(p);
		deqQ.addLast(q);

		while (!deqP.isEmpty()) {
			p = deqP.removeFirst();
			q = deqQ.removeFirst();

			if (!check(p, q)) {
				return false;
			}
			if (p != null) {
				// in Java nulls are not allowed in Deque
				if (!check(p.left, q.left)) {
					return false;
				}
				if (p.left != null) {
					deqP.addLast(p.left);
					deqQ.addLast(q.left);
				}
				if (!check(p.right, q.right)) {
					return false;
				}
				if (p.right != null) {
					deqP.addLast(p.right);
					deqQ.addLast(q.right);
				}
			}
		}
		return true;
	}
}

 

LeetCode_100. Same Tree

标签:class   tput   amp   ||   junit   java   sid   new t   ret   

原文地址:https://www.cnblogs.com/denggelin/p/11601193.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!