[Leetcode] 100. 相同的树

题目链接 : https://leetcode-cn.com/problems/same-tree/

题目描述:

给定两个二叉树,编写一个函数来检验它们是否相同。

如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。

示例:

示例 1:

输入:       1         1
          / \       / \
         2   3     2   3
        [1,2,3],   [1,2,3]
输出: true        

示例 2:

输入:      1          1
          /           \
         2             2    
   	 [1,2],     [1,null,2]
输出: false   	 

示例 3:

输入:       1         1
          / \       / \
         2   1     1   2  
        [1,2,1],   [1,1,2]
输出: false

思路:

直接看代码!

思路一:递归

思路二:迭代,看先序遍历是否一样

代码:

思路一:递归

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        if not p and not q: return True
        if p and q and p.val == q.val :
            return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right)     
        return False

java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        if (p == null && q == null) return true;
        if (p != null && q != null && p.val == q.val) return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
        else return false;
    }
}

思路二:

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution:
    def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
        stack = [(q, p)]
        while stack:
            a, b = stack.pop()
            if not a and not b:
                continue
            if a and b and a.val == b.val:
                stack.append((a.left, b.left))
                stack.append((a.right,b.right))
            else:
                return False
        return True

java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public boolean isSameTree(TreeNode p, TreeNode q) {
        Deque<TreeNode> stack1 = new LinkedList<>();
        Deque<TreeNode> stack2 = new LinkedList<>();
        stack1.push(p);
        stack2.push(q);
        while (!stack1.isEmpty() && !stack2.isEmpty()) {
            TreeNode a = stack1.pop();
            TreeNode b = stack2.pop();
            if (a == null && b == null) continue;
            if (a != null && b != null && a.val == b.val) {
                stack1.push(a.left);
                stack1.push(a.right);
                stack2.push(b.left);
                stack2.push(b.right);
            } else return false;
        }
        return stack1.isEmpty() && stack2.isEmpty();
    }
}

posted on 2019-06-25 20:06  威行天下  阅读(144)  评论(0编辑  收藏  举报

导航