当前位置:网站首页>Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)
Sword finger offer II 052. flatten binary search tree (simple binary search tree DFS)
2022-07-28 22:20:00 【Calm in the wind and rain】
The finger of the sword Offer II 052. Flatten the binary search tree
Here's a binary search tree , please Traverse in middle order Rearrange it into an incremental sequential search tree , Make the leftmost node in the tree the root node of the tree , And each node has no left child , There is only one right child node .
Example 1:

Input :root = [5,3,6,2,4,null,8,1,null,null,null,7,9]
Output :[1,null,2,null,3,null,4,null,5,null,6,null,7,null,8,null,9]
Example 2:

Input :root = [5,1,7]
Output :[1,null,5,null,7]
Tips :
The range of the number of nodes in the tree is [1, 100]
0 <= Node.val <= 1000
source : Power button (LeetCode)
link :https://leetcode-cn.com/problems/NYBBNL
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
analysis
Because each node of the flattened binary tree has only the right child node , Create a new tree, directly traverse in order, and put the nodes on the right side of the tree in order .
Answer key (Java)
/** * 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 {
private TreeNode ans = new TreeNode();
private TreeNode temp = new TreeNode();
public TreeNode increasingBST(TreeNode root) {
temp = ans;
dfs(root);
return ans.right;
}
private void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.left);
TreeNode node = new TreeNode(root.val);
temp.right = node;
temp = temp.right;
dfs(root.right);
}
}
边栏推荐
- 04.toRef 默认值
- 90. Subset II
- Summary of the use of hash table set and map when leetcode brushes questions
- Add DNS server to LAN for domain name resolution
- 第 7 篇:绘制旋转立方体
- HCIP(8)
- Kubeedge releases white paper on cloud native edge computing threat model and security protection technology
- HCIP(14)
- Learning notes and summary of C language programming specification
- get和post的区别
猜你喜欢
随机推荐
[LiteratureReview]Object Detection and Mapping with Bounding Box Constraints
Learning notes and summary of C language programming specification
Ordinary practice of JS DOM programming
Intranet penetration learning (III) horizontal movement of domain - planning tasks
Lvs+keepalived high availability deployment practical application
The applet listens for the target node to appear on the screen
tutorial/detailed_ workflow. Ipynb quantitative finance qlib Library
局域网添加DNS服务器进行域名解析
openresty 请求鉴权
Part 8: creating camera classes
Alibaba cloud CDN practice
Oracle triggers
Use pl/sql
Lin Xiaobin, head of Tencent cloud database, borrowed 100 million yuan to speculate in stocks? Insider: the amount is not true
Hcip experiment (12)
示波器发展史中的变化
迪赛智慧数——折线图(堆叠面积图):2022年不同职业人群存款额占月收入比例排名
What is time complexity
【机器学习】朴素贝叶斯对文本分类--对人名国别分类
SQL注入 Less42(POST型堆叠注入)









