How to solve the same tree problem of leetcode tree
This article mainly introduces how to solve the same tree problem of leetcode tree, which is very detailed and has certain reference value. Friends who are interested must finish reading it!
Given two binary trees, write a function to verify whether they are the same. If two trees are structurally identical and the nodes have the same value, they are considered to be the same. Example 1: input: 1 1 /\ /\ 2 3 2 3 [1 magi 2, 3 3], [1 recital 2, 3] output: true example 2: input: 1 1 /\ 2 [1 # 2], [1 recital null] 2] output: false example 3: input: 1 1 /\ /\ 2 1 1 2 [1 Magi 2 1], [1 Magi 1 2] output: false Source: LeetCode Link: https://leetcode-cn.com/problems/same-tree copyright belongs to the collar buckle network. For commercial reprint, please contact official authorization. For non-commercial reprint, please indicate the source. Answer the question / Definition for a binary tree node. * public class TreeNode {* int val; * TreeNode left; * TreeNode right; * TreeNode () {} * TreeNode (int val) {this.val = val;} * TreeNode (int val, TreeNode left, TreeNode right) {* this.val = val; * this.left = left; * this.right = right * / class Solution {public boolean isSameTree (TreeNode p, TreeNode Q) {if (p==null & & q==null) {return true;} if (paired null & & qpowered null & & p.val==q.val) {return isSameTree (p.left.q.left) & & isSameTree (p.right.q.right);} return false }} these are all the contents of the article "how to solve the same Tree problem of leetcode trees". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!