树
二叉树
题目描述
给你一个 二叉树 的根结点 root
,该二叉树由恰好 3
个结点组成:根结点、左子结点和右子结点。
如果根结点值等于两个子结点值之和,返回 true
,否则返回 false
。
示例 1:
输入: root = [10,4,6]
输出: true
解释: 根结点、左子结点和右子结点的值分别是 10 、4 和 6 。
由于 10 等于 4 + 6 ,因此返回 true 。
示例 2:
输入: root = [5,3,1]
输出: false
解释: 根结点、左子结点和右子结点的值分别是 5 、3 和 1 。
由于 5 不等于 3 + 1 ,因此返回 false 。
提示:
树只包含根结点、左子结点和右子结点
-100 <= Node.val <= 100
解法
方法一:直接判断
我们直接判断根节点的值是否等于左右子节点的值之和即可。
时间复杂度 $O(1)$,空间复杂度 $O(1)$。
Python3 Java C++ Go TypeScript Rust C
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution :
def checkTree ( self , root : Optional [ TreeNode ]) -> bool :
return root . val == root . left . val + root . right . val
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 /**
* 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 {
public boolean checkTree ( TreeNode root ) {
return root . val == root . left . val + root . right . val ;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 /**
* 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 checkTree ( TreeNode * root ) {
return root -> val == root -> left -> val + root -> right -> val ;
}
};
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func checkTree ( root * TreeNode ) bool {
return root . Val == root . Left . Val + root . Right . Val
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 /**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
function checkTree ( root : TreeNode | null ) : boolean {
return root . val === root . left . val + root . right . val ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 // Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option<Rc<RefCell<TreeNode>>>,
// pub right: Option<Rc<RefCell<TreeNode>>>,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std :: cell :: RefCell ;
use std :: rc :: Rc ;
impl Solution {
pub fn check_tree ( root : Option < Rc < RefCell < TreeNode >>> ) -> bool {
let node = root . as_ref (). unwrap (). borrow ();
let left = node . left . as_ref (). unwrap (). borrow (). val ;
let right = node . right . as_ref (). unwrap (). borrow (). val ;
node . val == left + right
}
}
1
2
3
4
5
6
7
8
9
10
11
12 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
bool checkTree ( struct TreeNode * root ) {
return root -> val == root -> left -> val + root -> right -> val ;
}
GitHub