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 does LeetCode solve the same tree problem

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

Share

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

Editor to share with you LeetCode how to solve the same tree problem, I believe that most people do not know much, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

1. Title

one hundred。 Same tree [1]

two。 Description

Given two binary trees, write a function to verify that 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:

Example 2:

Example 3:

3. Train of thought

Using the binary tree to do exercises to recursively traverse the framework, first operate on the root node, and then recursively the left and right child nodes.

Returns true when the current node of both trees is null

Returns false when one of them is null and the other is not null

Returns false when neither is empty but the values are not equal

The operation of the root node is completed in the above three steps, and then the left and right child nodes are recursive.

/ / Recursive access

Void traverse (TreeNode root) {

/ / preorder traversal, manipulating the root node first

Traverse (root.left)

/ / traversal in the middle order, manipulating the left child node

Traverse (root.right)

/ / traversal in post-order, manipulating the right child node

}

4. Implement public boolean isSameTree (TreeNodeTest p, TreeNodeTest Q) {

/ *

* determine the root node first

, /

/ / both are null, which means they are equal.

If (p = = null & & Q = = null) {

Return true

}

/ / one of them is null, which means it is not equal.

If (p = = null | | Q = = null) {

Return false

}

/ / neither of them is null, but the values are different, which means they are not equal.

If (p.val! = q.val) {

Return false

}

/ / after the root node is judged, the left and right nodes are judged

Return isSameTree (p.left, q.left) & & isSameTree (p.right, q.right)

}

The above is all the content of the article "how to solve the same tree problem with LeetCode". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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