当前位置:网站首页>[binary tree] verify binary tree
[binary tree] verify binary tree
2022-07-22 08:06:00 【How cold it is】
0x00 subject
There are... On the binary tree n
Nodes , Press from 0
To n - 1
Number
Where nodes i
The two child nodes of are leftChild[i]
and rightChild[i]
Only all
Nodes can form and only
formation One
Efficient binary tree
return true
; Otherwise return to false
If node i
There is no left child node , that leftChild[i]
Is equal to -1
The right child node also complies with this rule
Be careful : Node has no value , Only node numbers are used in this problem
0x01 Ideas
First of all, find root
node
And then again Traverse
Check if there is Ring
0x02 solution
Language :Swift
Tree node :TreeNode
public class TreeNode {
public var val: Int
public var left: TreeNode?
public var right: TreeNode?
public init() { self.val = 0; self.left = nil; self.right = nil; }
public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
self.val = val
self.left = left
self.right = right
}
}
solution :
func validateBinaryTreeNodes(_ n: Int, _ leftChild: [Int], _ rightChild: [Int]) -> Bool {
// Traverse 2 An array , Count the number of times
var arr = [Int](repeating: 0, count: n)
for i in 0..<n {
let left = leftChild[i]
if left != -1 {
arr[left] += 1
}
let right = rightChild[i]
if right != -1 {
arr[right] += 1
}
}
// Find the root node
var root = -1
for i in 0..<n {
if arr[i] == 0 {
root = i
break
}
}
if root == -1 {
return false
}
// Traverse , Check whether there are rings ( A node is accessed many times )
var count = 0
var queue: [Int] = []
var visited = [Int](repeating: 0, count: n)
queue.append(root)
visited[root] += 1
while !queue.isEmpty {
let node = queue.removeFirst()
count += 1
let left = leftChild[node]
if left != -1 {
visited[left] += 1
if visited[left] > 1 {
return false
}
queue.append(left)
}
let right = rightChild[node]
if right != -1 {
visited[right] += 1
if visited[right] > 1 {
return false
}
queue.append(right)
}
}
// Each node should traverse to
return count == n
}
0x03 My little work
Welcome to experience one of my works : Small editor -XCompiler
Small online editor App Store
Search ~
边栏推荐
- web3再牛 也沒能逃出這幾個老巨頭的手掌心
- 并发编程之多线程基础
- 单片机入门:LED灯循环左移点亮
- Introduction to single chip microcomputer: LED lights cycle to the right and turn on
- Unity SKFramework框架(一)、Audio音频管理器
- 微信支付Native(一)准备和相关知识
- 还在用 ListView?使用 AnimatedList 让列表元素动起来
- Android-SQLite数据库实战
- Read the paper with me - multi model text recognition network
- Differences among mdladdress, userbuffer and systembuffer of IRP structure
猜你喜欢
wallys/new product/DR7915/MT7915+MT7975/WiFi6 MiniPCIe Module 2T2R
你离「TDengine 开发者大会」只差一条 SQL 语句!
Wechat side: what is consistent hash, usage scenarios, and what problems have been solved?
Wechat payment native (I) preparation and related knowledge
MLX90640 红外热成像仪测温模块开发笔记(三)
Dynamics crm: encountered "the plug-in execution failed because no sandbox hosts are currently available“
Dynamics crm: xrmtoolbox plug-in recommendation
tsconfig. JSON cannot find any input in the configuration file. What should I do?
极客星球丨字节跳动一站式数据治理解决方案及平台架构
Let me show you eight fallacies in software design
随机推荐
并发编程之多线程基础
单片机入门:点亮第一个LED小灯
数据库学习 – select(多表联查)[通俗易懂]
还在用 ListView?使用 AnimatedList 让列表元素动起来
DS图—图的最短路径(不含代码框架)
2022 年PMP备考三大流程--应战篇(1)
JS数据转换 —— 树形结构和平铺结构的转换
After reading this, I can't DVMA yet. Please eat melon
Introduction to single chip microcomputer: light up multiple LED lights
Leetcode 1288. 删除被覆盖区间(可以,已解决)
微信小程序 wx.request的简单封装
看完这个,还不会DVMA,请你吃瓜
迪拜推出国家元宇宙战略
The relevant person in charge of the state Internet Information Office answered reporters' questions on the decision to impose administrative penalties related to network security review on didi Globa
Introduction to single chip microcomputer: light up the first LED light
Didi received 8billion fines, and the data security sector rose sharply. Qianxin rose 5.6% in midday trading
Dynamics crm: relationships (2) - n:n many to many
Kubernetes static storage and dynamic storage
怎样同构+跨端,懂得小程序+kbone+finclip就够了!
单片机入门:LED灯循环左移点亮