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 flip a python binary tree

2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

Today, I will talk to you about how to flip the python binary tree. Many people may not know much about it. In order to make you understand better, the editor has summarized the following contents for you. I hope you can get something from this article.

Order

Record the flip binary tree

The title flips a binary tree. Example: input: 4 /\ 2 7 /\ /\ 1 3 6 9 output: 4 /\ 7 2 /\ /\ 9 6 3 1 Note: this question is inspired by the original question of Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew) But you can't write the question of flipping the binary tree on the whiteboard during the interview, which is too bad. Source: LeetCode link: https://leetcode-cn.com/problems/invert-binary-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 (int x) {val = x;} *} * / class Solution {public TreeNode invertTree (TreeNode root) {if (root = = null) {return root;} TreeNode tmp = root.left; root.left = invertTree (root.right); root.right = invertTree (tmp) Return root;}}

The idea of recursion is adopted here. First, save the left node with tmp, then recursively flip the right node to assign to left, and then recursively flip the tmp assignment to right.

After reading the above, do you have any further understanding of how to flip the python binary tree? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

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