JavaScript刷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.

二、题目大意

  判断一颗二叉树中所有节点的值是不是相同。

三、解题思路

  递归处理

四、代码实现

const isUnivalTree = root => {
  if (!root) {
    return true
  }

  let pre = null
  let ans = true
  help(root)
  return ans
  function help (root) {
    if (!root) {
      return
    }

    if (!pre) {
      pre = root.val
    } else if (pre && pre !== root.val) {
      ans = false
      return
    }

    help(root.left)
    help(root.right)
  }
}

  如果本文对您有帮助,欢迎关注微信公众号,为您推送更多内容,ε=ε=ε=┏(゜ロ゜;)┛。

猜你喜欢

转载自blog.csdn.net/dai_qingyun/article/details/85534971