[Leetcode -2236. Determine whether the root node is equal to the sum of child nodes -2331. Calculate the value of a Boolean binary tree]

Leetcode -2236. Determine whether the root node is equal to the sum of child nodes

Question: You are given the root node root of a binary tree. The binary tree consists of exactly 3 nodes: the root node, the left child node and the right child node.
If the root node value is equal to the sum of the two child node values, return true, otherwise return false.

Example 1:
Insert image description here

Input: root = [10, 4, 6]
Output: true
Explanation: The values ​​of the root node, left sub-node and right sub-node are 10, 4 and 6 respectively.
Since 10 is equal to 4 + 6, it returns true.

Example 2:
Insert image description here

Input: root = [5, 3, 1]
Output: false
Explanation: The values ​​of the root node, left sub-node and right sub-node are 5, 3 and 1 respectively.
Since 5 is not equal to 3 + 1, false is returned.

Tip:
The tree only contains the root node, left child node and right child node

  • 100 <= Node.val <= 100

Idea : Directly return to determine whether the value of the root is equal to the sum of the val of the left node and the right node;

		bool checkTree(struct TreeNode* root)
		{
		    //直接返回判断根的值是否等于左节点和右节点 val 之和
		    return root->val == root->left->val + root->right->val;
		}

Leetcode -2331. Calculating the value of a Boolean binary tree

Question: Give you the root of a complete binary tree. This tree has the following characteristics:

Leaf nodes have either a value of 0 or a value of 1, where 0 represents False and 1 represents True.
Non-leaf nodes have either a value of 2 or a value of 3, where 2 represents logical OR and 3 represents logical AND.
The value of a node is calculated as follows:

If the node is a leaf node, the value of the node is itself, True or False.
Otherwise, the node values ​​of both children are calculated, and then that node's operator is applied to the two child values.
Returns the Boolean operation value of the root node root.

A complete binary tree is a binary tree in which each node has 0 or 2 children.
Leaf nodes are nodes that have no children.

Example 1:
Insert image description here

Input: root = [2, 1, 3, null, null, 0, 1]
Output: true
Explanation: The above figure shows the calculation process.
The value of the AND operation node is False AND True = False.
The value of the OR operation node is True OR False = True.
The value of the root node is True, so we return true.

Example 2:
Input: root = [0]
Output: false
Explanation: The root node is a leaf node and the value is false, so we return false.

Tip:
The number of nodes in the tree is between [1, 1000].
0 <= Node.val <= 3
The number of children of each node is 0 or 2.
The value of leaf nodes is 0 or 1.
The value of non-leaf nodes is 2 or 3.

Idea : If val == 2 or 3, return the logical operation corresponding to the left subtree and right subtree. If it is 0 or 1, return false or true;

		bool evaluateTree(struct TreeNode* root)
		{
		    //节点的值为1,返回true
		    if (root->val == 1)
		        return true;
		
		    //节点的值为0,返回false
		    if (root->val == 0)
		        return false;
		
		    //节点的值为2,将左子树和右子树进行逻辑或计算
		    if (root->val == 2)
		        return evaluateTree(root->left) || evaluateTree(root->right);
		
		    //节点的值为3,将左子树和右子树进行逻辑与计算
		    return evaluateTree(root->left) && evaluateTree(root->right);
		}

Guess you like

Origin blog.csdn.net/YoungMLet/article/details/131329594