码迷,mamicode.com
首页 > 其他好文 > 详细

965. Univalued Binary Tree

时间:2019-01-02 01:19:17      阅读:228      评论:0      收藏:0      [点我收藏+]

标签:tree   param   boolean   node   else   length   .com   null   tps   

/**

965. Univalued Binary Tree
* https://leetcode.com/problems/univalued-binary-tree/description/

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.

*/

/**
* @param {TreeNode} root
* @return {boolean}
*/
var isUnivalTree = function(root) {
  let stack = [];

  let lastValue = -1;

  while (stack.length > 0 || root != null) {
    if (root != null) {
      stack.push(root);
      root = root.left;
    } else {
      root = stack.pop();
      if (lastValue != -1) {
        if (lastValue != root.val)
          return false;
      }
     lastValue = root.val;
     root = root.right;
    }
  }
  return true;

};

965. Univalued Binary Tree

标签:tree   param   boolean   node   else   length   .com   null   tps   

原文地址:https://www.cnblogs.com/johnnyzhao/p/10206639.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!