当前位置:网站首页>Leetcode-226. Invert Binary Tree
Leetcode-226. Invert Binary Tree
2022-07-07 07:30:00 【Eistert】
subject
Given the root of a binary tree, invert the tree, and return its root.
Example 1:
Input: root = [4,2,7,1,3,6,9]
Output: [4,7,2,9,6,3,1]
Example 2:
Input: root = [2,1,3]
Output: [2,3,1]
Example 3:
Input: root = []
Output: []
Constraints:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
solution
Method 1 : recursive
Ideas and algorithms
This is a classic binary tree problem . obviously , We start at the root node , Recursively traverse the tree , And start flipping from the leaf node . If the node currently traversed root The two subtrees on the left and right have been turned over , Then we just need to exchange the positions of the two subtrees , Can be completed to root Is the flip of the entire subtree of the root node .
Code
/** * 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 TreeNode invertTree(TreeNode root) {
if(root == null){
return null;
}
TreeNode left = invertTree(root.left);
TreeNode right = invertTree(root.right);
root.right = left;
root.left = right;
return root;
}
}
source
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/invert-binary-tree
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
边栏推荐
- Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.10 tabbar component (I) how to open and use the default tabbar comp
- PostgreSQL source code (59) analysis of transaction ID allocation and overflow judgment methods
- A concurrent rule verification implementation
- How to * * labelimg
- My ideal software tester development status
- Explain Bleu in machine translation task in detail
- ROS2规划系统plansys2简单的例子
- Torefs API and toref API
- Role of virtual machine
- Bi she - college student part-time platform system based on SSM
猜你喜欢
抽丝剥茧C语言(高阶)数据的储存+练习
LC interview question 02.07 Linked list intersection & lc142 Circular linked list II
Mutual conversion between InputStream, int, shot, long and byte arrays
Detailed explanation of transform origin attribute
Convolutional neural network -- understanding of pooling
计算机服务中缺失MySQL服务
URP - shaders and materials - light shader lit
freeswitch拨打分机号源代码跟踪
三、高质量编程与性能调优实战 青训营笔记
transform-origin属性详解
随机推荐
Wechat applet full stack development practice Chapter 3 Introduction and use of APIs commonly used in wechat applet development -- 3.9 introduction to network interface (IX) extending the request3 met
抽絲剝繭C語言(高階)數據的儲存+練習
Lm11 reconstruction of K-line and construction of timing trading strategy
Docker compose start redis cluster
js小练习----分时提醒问候、表单密码显示隐藏效果、文本框焦点事件、关闭广告
Flexible layout (II)
弹性布局(一)
Implementing data dictionary with JSP custom tag
MySQL service is missing from computer service
Abnova membrane protein lipoprotein technology and category display
js小练习
Redis data migration
Blue Bridge Cup Birthday candles (violence)
sql中对集合进行非空校验
Advanced level of C language (high level) pointer
基于Flask搭建个人网站
聊聊异步编程的 7 种实现方式
leetcode 509. Fibonacci number
OOM(内存溢出)造成原因及解决方案
ViewModelProvider. Of obsolete solution