Leetcode学习笔记:#965. Univalued Binary Tree

Leetcode学习笔记:#965. Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value.

Return true if and only if the given tree is univalued.

实现:

public boolean isUnivalTree(TreeNode root){
	return (root.left == null || root.left.val == root.val && isUnivalTree(root.left)) &&
               (root.right == null || root.right.val == root.val && isUnivalTree(root.right));
    }

思路:
递归,如果该根节点有子节点,则每个节点的值就应该相等。

猜你喜欢

转载自blog.csdn.net/ccystewart/article/details/90208054