当前位置:网站首页>Summary 623 of the force buckle solution - add a row to the binary tree
Summary 623 of the force buckle solution - add a row to the binary tree
2022-08-05 13:32:00 【lost summer】
Directory link:
Likou Programming Questions-Solution Summary_Share + Record-CSDN Blog
GitHub synchronization brushing project:
https://github.com/September26/java-algorithms
Original link:
Leave
Description:
Given a binary tree root root and two integers val and depth, add a row of nodes with value val at the given depth depth.
Note that the root node root is at depth 1.
The addition rules are as follows:
Given an integer depth, for each non-empty tree node cur of depth - 1, create two tree nodes of value val as the left and right sub-roots of cur .
cur The original left subtree should be the left subtree of the root of the new left subtree.
cur The original right subtree should be the right subtree of the root of the new right subtree.
If depth == 1 means that depth - 1 has no depth at all, then create a tree node with value val as the new root of the entire original tree, which is the left subtree of the new root.
Example 1:
Input: root = [4,2,6,3,1,5], val = 1, depth = 2
Output: [4,1,1,2,null,null,6,3,1,5]
Example 2:
Input: root = [4,2,null,3,1], val = 1, depth = 3
Output: [4,2,null,1,1,3,null,null,1]
Tips:
The number of nodes is in the range [1, 104]
The depth of the tree is in the range [1, 104]
-100 <= Node.val <= 100
-105 <= val <=105
1 <= depth <= the depth of tree + 1
Source: LeetCode
Link: https://leetcode.cn/problems/add-one-row-to-tree
The copyright belongs to LeetCode.com.For commercial reprints, please contact the official authorization, and for non-commercial reprints, please indicate the source.
Solution ideas:
* Problem-solving ideas:* Recursive judgment, divided into three scenarios:* When depathlevel, recursive judgment is required.It should be noted here that if the root itself has only three levels, but when depth=4, nodes should also be added.So handle the scenario where root.left == null and depth == level + 1* When depth == level, you need to judge ifLeft. If it is true, parentNode.left = newNode; and newNode.left = root;, and the operation is similar when false.
Code:
public class Solution623 {public TreeNode addOneRow(TreeNode root, int val, int depth) {return addOneRowByLevel(root, val, depth, 1, true, null);}private TreeNode addOneRowByLevel(TreeNode root, int val, int depth, int level, boolean isLeft, TreeNode parentNode) {if (depth < level) {return root;}if (depth > level) {if (root.left != null) {addOneRowByLevel(root.left, val, depth, level + 1, true, root);} else if (depth == level + 1) {root.left = new TreeNode(val);}if (root.right != null) {addOneRowByLevel(root.right, val, depth, level + 1, false, root);} else if (depth == level + 1) {root.right = new TreeNode(val);}return root;}TreeNode newNode = new TreeNode(val);if (isLeft) {newNode.left = root;if (parentNode != null) {parentNode.left = newNode;}} else {newNode.right = root;parentNode.right = newNode;}return newNode;}}边栏推荐
猜你喜欢

阿里二面:明明加了唯一索引,为什么还是产生重复数据?

爱可可AI前沿推介(8.5)

Jiang: in the second half of 2022 the 22 rule to buck the trend

一行简单的样式,让网页有「高级感」

十分钟教会你如何使用VitePress搭建及部署个人博客站点

买个社区团购小程序多少钱呢?微信社区团购小程序怎么做

IIoT系统架构

EMQ & IoTDB 联合 Meetup 杭州站 | 8月13日线下场报名开启

RT-Thread recording (2. RT-Thread kernel startup process - startup file and source code analysis)

【CC3200AI 实验教程2】疯壳·AI语音人脸识别(会议记录仪/人脸打卡机)-系统测试
随机推荐
DSPE-PEG-Silane,DSPE-PEG-SIL,磷脂-聚乙二醇-硅烷可修饰材料表面
【Search box】General test case
地平线初体验.下
第一章 调度系统架构设计之线程池创建
爱可可AI前沿推介(8.5)
十分钟教会你如何使用VitePress搭建及部署个人博客站点
115. 关于将本地 SAP UI5 应用配置到本地 Fiori Launchpad 的技术实现深入讲解
模拟实现(strncpy)(strncat)(atoi)(C语言)(VS)
Qt将图片保存为XML文件或者变为QSting
2022-08-03 顾宇佳 学习笔记
松翰烧录器在keil仿真时闪退,解决方法
思岚S1激光雷达
记一次 .NET 某工控自动化控制系统 卡死分析
运力升级助力算力流转,中国数字经济的加速时刻
1236288-25-7,DSPE-PEG-FA,Folic acid PEG DSPE,磷脂-聚乙二醇-叶酸脂质体形成材料
《MySQL核心知识》第6章:查询语句
为什么鲜有炫富的程序员?
VINS-Mono结果展示
Amazon Detective 支持 Amazon EKS 上的 Kubernetes 工作负载以进行安全调查
EAI X2(非订制版)50一个激光雷达?