当前位置:网站首页>Leetcode-6116: calculate the value of Boolean binary tree
Leetcode-6116: calculate the value of Boolean binary tree
2022-07-22 13:33:00 【Chrysanthemum headed bat】
leetcode-6116: Calculate the value of Boolean binary tree
subject
Topic linking
Here's a tree for you Complete binary tree The root of the , This tree has the following characteristics :
- Leaf node Or the value is 0 Or the value is 1 , among 0 Express False ,1 Express True .
- Nonleaf node Or the value is 2 Or the value is 3 , among 2 Represent logical or OR ,3 Representation logic and AND .
Calculation The value of a node is as follows :
- If the node is a leaf node , Then the node value For itself , namely True perhaps False .
- otherwise , Calculation Node value of two children , Then, the operator of this node is used to compare two child values operation .
Return root node root Boolean operation value of .
Complete binary tree Each node has 0 A or 2 A child's binary tree .
Leaf node It's a node without children .
Example 1:
Input :root = [2,1,3,null,null,0,1]
Output :true
explain : The figure above shows the calculation process .
AND The value of and operation node is False AND True = False .
OR The value of the operation node is True OR False = True .
The value of the root node is True , So we go back to true .
Example 2:
Input :root = [0]
Output :false
explain : The root node is the leaf node , And the value is false, So we go back to false .
Problem solving
Method 1 : After the sequence traversal
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
bool dfs(TreeNode* root){
if(root->val==0) return false;
if(root->val==1) return true;
bool left=dfs(root->left);
bool right=dfs(root->right);
if(root->val==2){
return left||right;
}
else if(root->val==3){
return left&&right;
}
return true;
}
bool evaluateTree(TreeNode* root) {
return dfs(root);
}
};
边栏推荐
- LeetCode_ Dynamic programming_ Difficulties_ 44. Wildcard matching
- [MySQL]索引事务
- ApacheCon Asia 2022 开启报名:Pulsar 技术议题重磅亮相
- [mysql] basic database operations
- 如何正确使用call、bind、apply
- Analysis of the advantages of the LAAS scheme of elephant swap led to strong performance of ETOKEN
- QT program packaging and publishing method (using the official windeployqt tool)
- Addition, deletion, query and modification of [mdsql] table (Advanced)
- leetcode-307:区域和检索 - 数组可修改
- [PostgreSQL 15] PostgreSQL 15 improves unique and null
猜你喜欢
Uniapp realizes the lucky circle function of lottery
MySQL basic functions
One bite of Stream(9)
Elephant Swap的LaaS方案优势分析,致eToken表现强势
Service (LB) and managed pod
How to wirelessly transmit the liquid level value of asphalt high-level tank to the heat carrier recorder for monitoring?
自动化测试简历编写应该注意哪方面?有哪些技巧?
How to use call, bind and apply correctly
Border dynamic effect implementation
Talk about the top 10 mistakes often made in implementing data governance
随机推荐
MySQL basic functions
2022.7.21-----leetcode.814
直播回顾| Apache Pulsar Meetup 精彩回放(含 PPT 下载)
[advanced C language] learning about flexible arrays
(iclr-2021) an image is equivalent to 16x16 words: a transformer for large-scale image recognition
WebService interface test
自动化测试简历编写应该注意哪方面?有哪些技巧?
2022/07/19 --- stack push in and pop-up sequence; String representing numeric value
解析云原生消息流系统 Apache Pulsar 能力及场景
leetcode-140:单词拆分 II
How to wirelessly transmit the liquid level value of asphalt high-level tank to the heat carrier recorder for monitoring?
边框动态效果实现
Border dynamic effect implementation
One bite of Stream(8)
2022/07/18------顺时针打印矩阵
如何安装mysql
Addition, deletion, query and modification of [mysql] table (basic)
leetcode-735:行星碰撞
Messy analysis view system
[mysql] basic database operations