zoukankan      html  css  js  c++  java
  • 965. Univalued Binary Tree

    /**

    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;

    };

  • 相关阅读:
    bzoj2161 布娃娃
    bzoj2161 布娃娃
    Tyvj1054
    Tyvj1054
    Tyvj1053
    Tyvj1053
    hdu3265 Poster(扫描线)
    hdu3265 Poster(扫描线)
    hdu3265(好题翻译)
    hdu3265(好题翻译)
  • 原文地址:https://www.cnblogs.com/johnnyzhao/p/10206639.html
Copyright © 2011-2022 走看看