Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the same tree problem of leetcode tree

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report